Each of your goroutines spawned from Mux
ends up pulling from the same channel, since c
gets updated on each iteration of the loop – they don't just capture the value of c
. You will get the expected results if you pass the channel to the goroutine like so:
for _, c := range channels {
go func(c <-chan big.Int) {
...
}(c)
}
You can test this modification here.
One other possible problem is your handling of the n
variable: if you're running with GOMAXPROCS != 1
, you could have two goroutines trying to update it at once. The sync.WaitGroup
type would be a safer way to wait for goroutines to complete.