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