Skip to content

Instantly share code, notes, and snippets.

@mnadel
Created November 7, 2014 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnadel/aae1b4092cb2c5b0d521 to your computer and use it in GitHub Desktop.
Save mnadel/aae1b4092cb2c5b0d521 to your computer and use it in GitHub Desktop.
Golang Concurrency Pattern
package main
import (
"sync"
"fmt"
)
func main() {
ch := make(chan int)
var wg sync.WaitGroup
wg.Add(1)
go func() {
for i := 0; i < 10; i++ {
ch <- i
}
wg.Done()
}()
wg.Add(1)
go func() {
for i := 10; i < 20; i++ {
ch <- i
}
wg.Done()
}()
go func() {
wg.Wait()
close(ch)
}()
for i := range ch {
fmt.Println(i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment