我正在学习 Go 语言。这是我遇到的一个例子。有人可以解释一下这里发生了什么吗?
package main
import "time"
import "fmt"
func main() {
c1 := make(chan string)
c2 := make(chan string)
go func() {
time.Sleep(time.Second * 1)
c1 <- "one"
}()
go func() {
time.Sleep(time.Second * 2)
c2 <- "two"
}()
for i := 0; i < 2; i++ {
select {
case msg1 := <-c1:
fmt.Println("received", msg1)
case msg2 := <-c2:
fmt.Println("received", msg2)
default:
fmt.Println("Default")
}
}
}
输出:
Default
Default
Program Exited
如果我注释掉默认部分
//default:
// fmt.Println("Default")
输出变为:
received one
received two
Program exited.
default案例的存在如何改变通道阻塞的工作方式?
慕的地6264312
拉莫斯之舞
相关分类