package main
func main() {
c := make(chan int, 1)
for done := false; !done; {
select {
default:
print(1)
done = true
case <-c:
print(2)
c = nil
case c <- 1:
print(3)
}
}
}
Choices:
Answer: 321
Run it on Go play.
Key points:
c <- 1
is non-blocking.
So the last case
branch is chosen.
<-c
is non-blocking.
So the first case
branch is chosen.
default
branch is chosen.
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.