Skip to content

Instantly share code, notes, and snippets.

@alexchowle
Created February 7, 2016 20:58
Show Gist options
  • Save alexchowle/0a7f9757afd2c895fe3c to your computer and use it in GitHub Desktop.
Save alexchowle/0a7f9757afd2c895fe3c to your computer and use it in GitHub Desktop.
Golang concurrency - await first returned
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