https://play.golang.org/p/FMKxtVlTL5
Why does the default:
in the select statement infinite loop the program?
package main
import (
"fmt"
"strconv"
"time"
)
var quit chan bool
var counter chan int
func main() {
counter = make(chan int)
quit = make(chan bool)
go func() {
i := 0
for {
i++
select {
case <-quit:
fmt.Println("Bye!")
return
case counter <- i:
fmt.Println("Send! " + strconv.Itoa(i))
default:
fmt.Println("Default! " + strconv.Itoa(i))
}
}
}()
fmt.Println("Receive! " + strconv.Itoa(<-counter))
fmt.Println("Receive! " + strconv.Itoa(<-counter))
fmt.Println("Receive! " + strconv.Itoa(<-counter))
fmt.Println("Receive! " + strconv.Itoa(<-counter))
fmt.Println("Receive! " + strconv.Itoa(<-counter))
fmt.Println("Receive! " + strconv.Itoa(<-counter))
quit <- true
time.Sleep(1 * time.Second)
}
When a select
has a default
, it becomes non-blocking. To quote the spec:
If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed.
Without the default
, the select
would wait until it could receive from quit
or send on counter
, do that, and then continue (going through the for
loop again, unless it ran that return
). With the default, the select
never waits for anything and the default
simply runs every time it can, unless one of the other operations goes through.
In theory the program still works the same, and will exit after the sixth time of printing "Send!" and "Receive!" — but it might be too busy printing "Default!" several billion times to get there in a reasonable time.