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
How do you set cookies in an HTTP request with an HTTP client in Go?
How to read current directory using Readdir?
Top Programming Languages Behind Blockchain App Development
How to initialize a struct containing a slice of structs in Golang?
Golang Concurrency Best Practices
Syntax error: unexpected <token> error in Golang
Golang program for implementation LZW Data Compression and Uncompression
Program in Go language to print Pascal's Triangle
Golang program for implementation of Levenshtein distance
Convert Float32 to Float64 and Float64 to Float32