Channels allow us to easily handle a scenario in which multiple consumers receive data from one producer and vice versa.
The case with a single producer and one consumer, as we have already seen, is pretty straightforward:
func main() {
// one producer
var ch = make(chan int)
go func() {
for i := 0; i < 100; i++ {
ch <- i
}
close(ch)
}()
// one consumer
var done = make(chan struct{})
go func() {
for i := range ch {
fmt.Println(i)
}
close(done)
}()
<-done
}
The full example is available here: https://play.golang.org/p/hNgehu62kjv.