receive from multiple channels golang code example
Example: receive from multiple channels golang
package main
import (
"fmt"
"sync"
)
func main() {
c1 := make(chan int)
c2 := make(chan int)
go sender(c1)
go sender(c2)
receiver(c1, c2)
}
func sender(c chan<- int) {
defer close(c)
for i := 0; i < 10; i++ {
c <- i
}
}
func receiver(cc ...<-chan int) {
var wg sync.WaitGroup
wg.Add(len(cc))
for _, c := range cc {
go func(c <-chan int) {
for v := range c {
// runtime.Gosched()
fmt.Println(v)
}
wg.Done()
}(c)
}
wg.Wait()
}