Send and Receive values from Channel
The main function has two functions generator and receiver. We create a c int channel and return it from the generator function. The for loop running inside anonymous goroutines writing the values to the channel c.
Example
package main
import (
"fmt"
)
func main() {
c := generator()
receiver(c)
}
func receiver(c <-chan int) {
for v := range c {
fmt.Println(v)
}
}
func generator() <-chan int {
c := make(chan int)
go func() {
for i := 0; i < 10; i++ {
c <- i
}
close(c)
}()
return c
}
0
1
2
3
4
5
6
7
8
9
Most Helpful This Week
Golang program for implementation LZW Data Compression and Uncompression
Top Programming Languages Behind Blockchain App Development
Golang program for implementation of Shell Sort
Creating a Struct Instance Using a Struct Literal
How to create Slice using new keyword in Golang?
How do you read headers from an HTTP response with an HTTP client in Go?
How to append struct member dynamically using Empty Interface?
Golang read json file into struct
Golang read csv file into struct
How do you send an HTTP PATCH request in Go?