Multiple consumers (1 * M)

The same reasoning applies with multiple consumers – they all receive from the same channel in different goroutines:

func main() {
// three consumers
wg := sync.WaitGroup{}
wg.Add(3)
var ch = make(chan string)

for i := 0; i < 3; i++ {
go func(n int) {
for i := range ch {
fmt.Println(n, i)
}
wg.Done()
}(i)
}

// one producer
go func() {
for i := 0; i < 10; i++ {
ch <- fmt.Sprintln("prod-", i)
}
close(ch)
}()

wg.Wait()
}

The full example is available here: https://play.golang.org/p/_SWtw54ITFn.

In this case, sync.WaitGroup is used to wait for the application to end.