I need to process several tasks concurrently and then "collect" the results. Below is the code I came up with but I'm wondering if it's the right way to do this(i.e idiomatic/ best practice) or if there is something wrong that I might miss.
package main
import "fmt"
import "sync"
func main() {
// ch is the int provider. Cap is 99 but it should
// really be 3
ch := make(chan int, 99)
var wg sync.WaitGroup
for i := 0; i < 3; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
ch <- i
}(i)
}
wg.Wait()
for v := range ch {
fmt.Println("consume ", v)
if len(ch) == 0 {
close(ch)
}
}
fmt.Printf("All done")
}