Skip to content

Instantly share code, notes, and snippets.

@suzuken
Created April 5, 2016 07:43
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 suzuken/353a0b50501f663423f8b684503a027b to your computer and use it in GitHub Desktop.
Save suzuken/353a0b50501f663423f8b684503a027b to your computer and use it in GitHub Desktop.
package main
import (
"time"
)
func main() {
print(q())
}
func q() int {
// when use unbuffered channel, only the fastest goroutine is consumed,
// and the rest of two will be leaked.
resp := make(chan int)
go func() {
time.Sleep(1 * time.Second)
resp <- 1
}()
go func() {
time.Sleep(2 * time.Second)
resp <- 2
}()
go func() {
time.Sleep(3 * time.Second)
resp <- 3
}()
return <-resp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment