Select 语句使用
1. select 介绍
select 介绍func server1(ch chan string) {
time.Sleep(1 * time.Second)
ch <- "from server 1"
}
func server2(ch chan string) {
time.Sleep(3 * time.Second)
ch <- "from server 2"
}
func main() {
out1 := make(chan string)
out2 := make(chan string)
go server1(out1)
go server2(out2)
select {
case s1 := <-out1:
fmt.Println(s1)
case s2 := <-out2:
fmt.Println(s2)
//default:
// fmt.Println(nil)
}
}
//去掉注释,则返回 nil1.1 default case
1.2 死锁以及default case
1.3 随机选择
Last updated