Depending on your requirements, you may need to read both channels for each iteration (ie, a function like "zip"). You can do this with select, similar to user860302 :
func main() { c1 := make(chan int) c2 := make(chan int) out := make(chan int) go func(in1, in2 <-chan int, out chan<- int) { for { sum := 0 select { case sum = <-in1: sum += <-in2 case sum = <-in2: sum += <-in1 } out <- sum } }(c1, c2, out) }
It works forever. My preferred way to finish mountains like this is to close the input channels. In this case, you will need to wait until both are closed, and then close(out) until completion.
Tip. Pay attention to the use of directional channels as formal parameters of goroutin. The compiler catches more errors when you write it like this. Happiness!
Rick-777
source share