Skip to content

Instantly share code, notes, and snippets.

@glesica
Created February 14, 2015 02:26
Show Gist options
  • Save glesica/7db5e0308589dbfe149a to your computer and use it in GitHub Desktop.
Save glesica/7db5e0308589dbfe149a to your computer and use it in GitHub Desktop.
package main
import "sync"
func main() {
jobChan := make(chan int)
resChan := make(chan int)
squareGroup := new(sync.WaitGroup)
// The workers
for i := 0; i < 5; i++ {
squareGroup.Add(1)
go func() {
for n := range jobChan {
resChan <- n * n
}
squareGroup.Done()
}()
}
// The "submitter"
go func() {
for i := 0; i < 10; i++ {
jobChan <- i
}
close(jobChan)
squareGroup.Wait()
close(resChan)
}()
// Collect the results back in the main goroutine
for s := range resChan {
println(s)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment