Skip to content

Instantly share code, notes, and snippets.

@ryochack
Created August 8, 2013 10:32
Show Gist options
  • Select an option

  • Save ryochack/6183571 to your computer and use it in GitHub Desktop.

Select an option

Save ryochack/6183571 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
)
var wg sync.WaitGroup
func f(msg string, n int) chan string {
ch := make(chan string)
go func() {
for i:=0; i<n; i++ {
ch <- msg + " please!"
}
wg.Done()
}()
return ch
}
func main() {
wg.Add(3)
ch1 := f("beer", 4)
ch2 := f("juice", 2)
ch3 := f("water", 1)
done := make(chan bool)
go func() {
wg.Wait()
done <- true
}()
L:
for {
select {
case msg := <-ch1:
fmt.Println(msg)
case msg := <-ch2:
fmt.Println(msg)
case msg := <-ch3:
fmt.Println(msg)
case <-done:
break L
}
}
fmt.Println("exit")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment