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:
- 10 goroutines are triggered and each of them writes a value in a channel.
- Another goroutine reads the channel and fills the slice.
- After all these stuff are done, the slice is printed.
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