Go-select实现超时
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package main 2 3 import ( 4 "fmt" 5 "time" 6 ) 7 8 func main() { 9 ch := make(chan int) 10 quit := make(chan bool) 11 12 //新开协程 13 go func() { 14 for { 15 select { 16 case num := <-ch: 17 fmt.Println("num:", num) 18 case <-time.After(3 * time.Second): 19 fmt.Println("超时") 20 quit <- true 21 } 22 } 23 }() 24 for i := 0; i < 3; i++ { 25 ch <- i 26 } 27 <-quit 28 fmt.Println("程序结束") 29 }