Created
February 7, 2016 20:58
-
-
Save alexchowle/0a7f9757afd2c895fe3c to your computer and use it in GitHub Desktop.
Golang concurrency - await first returned
This file contains 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" | |
"math/rand" | |
"time" | |
) | |
func main() { | |
ch := make(chan struct{}) | |
for i := 0; i < 3; i++ { | |
go slowRunning(ch) | |
} | |
<-ch | |
fmt.Println("Finishing.") | |
} | |
func slowRunning(ch chan<- struct{}) { | |
sleepLen := rand.Intn(5) | |
fmt.Printf("Sleeping for %d seconds.\n", sleepLen) | |
time.Sleep(time.Duration(sleepLen) * time.Second) | |
fmt.Println("Waking up...") | |
ch <- struct{}{} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment