I'm fairly new in Go and I want to run several tasks asynchronously, wait for all of them to be finished and collect their results into a slice.
I was reading a lot of documentation and examples, specially this Nathan LeClaire's post, and came up with something close to what I want to do (see code below). The mechanic is simple:
However the outcome shows a 9-length slice (values from 0 to 8) and the 10th value (should be 9) seems missing. The program exits just fine and I don't know what is going on. Any hint is appreciated.
Here is a code example to play with http://play.golang.org/p/HUFOZLmCto:
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
n := 10
c := make(chan int)
wg.Add(n)
for i := 0; i < n; i++ {
go func(val int) {
defer wg.Done()
fmt.Println("Sending value to channel: ", val)
c <- val
}(i)
}
var array []int
go func() {
for val := range c {
fmt.Println("Recieving from channel: ", val)
array = append(array, val)
}
}()
wg.Wait()
fmt.Println("Array: ", array)
}
And this is the outcome:
Sending value to channel: 0
Recieving from channel: 0
Sending value to channel: 1
Recieving from channel: 1
Sending value to channel: 2
Recieving from channel: 2
Sending value to channel: 3
Recieving from channel: 3
Sending value to channel: 4
Recieving from channel: 4
Sending value to channel: 5
Recieving from channel: 5
Sending value to channel: 6
Recieving from channel: 6
Sending value to channel: 7
Recieving from channel: 7
Sending value to channel: 8
Recieving from channel: 8
Sending value to channel: 9
Array: [0 1 2 3 4 5 6 7 8] // Note 9 is missing here
You're exiting before the receiving goroutine has a chance to receive and handle the value. There is also a race condition on the array
variable, where main
may try to print the array during the append
operation.
Note that even though an using unbuffered channel would create a synchronization point between the two loops, and guarantee that the receive loop has the value before wg.Done()
, it wouldn't guarantee that the fmt.Println
and append
happen before main
continues.
One way to arrange this is to put the receive loop in main, and wait on closing the c
chan
in it's own goroutine:
go func() {
wg.Wait()
close(c)
}()
for val := range c {
fmt.Println("Recieving from channel: ", val)
array = append(array, val)
}
Pretty simple. At some point all the values are written, the waitGroup is released and a goroutine is filling the sice. Since the waitGroup is release its possible for the print to occur before the channel is drained into the slice.
To solve, move the wg.Done() into the reader to prevent the print happening before the draining.
package main
import (
"fmt"
"sync"
)
func main() {
n := 10
c := make(chan int)
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < n; i++ {
go func(val int) {
fmt.Println("Sending value to channel: ", val)
c <- val
}(i)
}
var array []int
go func() {
for val := range c {
fmt.Println("Recieving from channel: ", val)
array = append(array, val)
wg.Done()
}
}()
wg.Wait()
fmt.Println("Array: ", array)
}
Example in playground