w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
A channel multiplexer

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.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.