select {
case ch <- 12:
... foo
default:
... bar
}
//编译后
if selectnbsend(ch, 12) {
... foo
} else {
... bar
}
runtime.selectnbsend也仅仅是调用了runtime.chansend,但是会标记上非阻塞。
func selectnbsend(c *hchan, elem unsafe.Pointer) (selected bool) {
return chansend(c, elem, false, getcallerpc())
}
func chansend(c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool
select {
case v = <-c:
... foo
default:
... bar
}
// 被编译器转化为
if selectnbrecv(&v, c) {
... foo
} else {
... bar
}
//comma ok写法
select {
case v, ok = <-c:
... foo
default:
... bar
}
// 被编译器转化为
if c != nil && selectnbrecv2(&v, &ok, c) {
... foo
} else {
... bar
}