[GO]使用select实现超时

package main

import (
"fmt"
"time"
) func main() {
ch := make(chan int)
quit := make(chan bool) go func() {
for true {
select {
case num := <- ch:
fmt.Println("num = ", num)
case <-time.After(*time.Second):
quit<-true
}
}
}()
for i:=; i<; i++ {
ch<-i
time.Sleep(time.Second)
}
<-quit
fmt.Println("程序结束")
}

打印的结果

num =
num =
num =
num =
num =
num =
num =
num =
num =
程序结束
上一篇:Spring实战(九)AOP概念以及Spring AOP


下一篇:【00NOIP普及组】税收与补贴问题(信息学奥赛一本通 1911)( 洛谷 1023)