Created
August 8, 2013 10:32
-
-
Save ryochack/6183571 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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