A short program about select
statement for channels.
package main
import "fmt"
func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case s := <-quit:
fmt.Println("quit =",s)
return
}
}
}
func main() {
c := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 10; i++ {
fmt.Println(<-c)
}
quit <- 9
}()
fibonacci(c, quit)
}
The result of the code above:
0
1
1
2
3
5
8
13
21
34
quit = 9
It worked fine. But after I changed (in func fibonacci
)
case s := <-quit:
fmt.Println("quit =",s)
to
case <-quit:
fmt.Println(<-quit)
an fatal error occurred:
0
1
1
2
3
5
8
13
21
34
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.fibonacci(0x18348040, 0x18348080)
/tmp/compile42.go:12 +0xf9
main.main()
/tmp/compile42.go:27 +0x11c
Where does the error come from?
In second case you are getting the value form the channel two times. Every time you do something like <-channel you pop one value out of channel.
Hence program is waiting indefinitely on line
fmt.Println(<-quit)
but fortunately go is intelligent enough to detect this situation and panic with error "all goroutines are asleep - deadlock!"
The line
fmt.Println(<-quit)
is waiting for another value on the channel, which will never come, according to the code you have.
You have to keep in mind the line before in the select:
case s := <-quit
has already removed the quit value from the channel.
Thus it will never complete.