select语句可以有一个可选默认case,它必须出现在select的结束前。缺省情况下,可用于执行任务时没有的情况下是真实的。在默认情况下break不是必需的。
package main
import "fmt"
func main() {
var c1, c2, c3 chan int
var i1, i2 int
select {
case i1 = -c1:
fmt.Printf("received ", i1, " from c1\n")
case c2 - i2:
fmt.Printf("sent ", i2, " to c2\n")
case i3, ok := (-c3): // same as: i3, ok := -c3
if ok {
fmt.Printf("received ", i3, " from c3\n")
} else {
fmt.Printf("c3 is closed\n")
}
default:
fmt.Printf("no communication\n")
}
}