Hi, everybody I have a problem with merging channels, I am trying to solve a multithreading task, but testing system always gives Wrong Answer on any of my attempts. So I have code which should read values from both channels and put f(a) + f(b) to the output, n times. My basic idea presented below. It seems to work for my tests but can’t pass even the first in test system. May someone please point out mistake in my logic or give any ideas on possible solutions. Thanks!
func merge(f func(int) int, ch1 <-chan int, ch2 <-chan int, out chan int, n int) {
go func() {
defer close(out)
for i := 0; i < n; i++ {
select {
case v := <-ch1:
out <- (f(v) + f(<-ch2))
case v := <-ch2:
out <- (f(v) + f(<-ch1))
}
}
}()
}