Answers for "receive from multiple channels golang"

Go
0

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()
}
Posted by: Guest on May-29-2020

Code answers related to "receive from multiple channels golang"

Browse Popular Code Answers by Language