go语言中go+select的理解
生活随笔
收集整理的這篇文章主要介紹了
go语言中go+select的理解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// select在go語言協程里面是有阻塞select后面繼續運行的代碼作用,但是當select的case獲取到數據之后,協程里面的代碼是不會停止的,他還是會繼續運行的
package mainimport("fmt""time" )func main(){fmt.Println("超時設置")var ch chan stringgo func() {time.Sleep(time.Second*3)fmt.Println("dasfsdfaf") //這里還是會運行的}()select {case res := <-ch:fmt.Println(res)return case <-time.After(time.Second * 2): //即使時間過了2秒之后,協程里面的代碼還是會運行到time.Sleep(time.Second*3) fmt.Println("timeout")} }
package mainimport("fmt""time" )func main(){fmt.Println("超時設置")var ch chan stringgo func() {time.Sleep(time.Second*3)fmt.Println("dasfsdfaf") //這里還是會運行的}()select {case res := <-ch:fmt.Println(res)return case <-time.After(time.Second * 2): //即使時間過了2秒之后,協程里面的代碼還是會運行到time.Sleep(time.Second*3) fmt.Println("timeout")} }
轉載于:https://www.cnblogs.com/MyUniverse/p/11234984.html
總結
以上是生活随笔為你收集整理的go语言中go+select的理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常见的权限访问控制模型
- 下一篇: Office 2010 与搜狗输入法兼容