Created
April 5, 2016 07:43
-
-
Save suzuken/353a0b50501f663423f8b684503a027b to your computer and use it in GitHub Desktop.
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 ( | |
"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