package main
import (
"fmt"
"time"
)
func main() {
intChan := make(chan int, 1)
time.AfterFunc(time.Second*5,func(){
intChan<- 99
})
time.AfterFunc(time.Second*10,func(){
close(intChan)
})
var kai = true
for kai {
select {
case i, ok := <-intChan:
fmt.Println("这个通道的case1被select选中了。", i)
if !ok {
fmt.Println("通道关闭了。")
kai=false //退出外层for循环
}
case i, ok := <-intChan:
fmt.Println("这个通道的case2被select选中了。", i)
if !ok {
fmt.Println("通道关闭了。")
kai=false //退出外层for循环
}
}
//上面两case都满足,那么程序会随机选择一个,然后退出select。
}
time.Sleep(time.Second*20)
}
GO通道select外面套for循环读取通道的值
最新推荐文章于 2024-05-11 03:51:40 发布