// select在go語言協程里面是有阻塞select后面繼續運行的代碼作用,但是當select的case獲取到數據之后,協程里面的代碼是不會停止的,他還是會繼續運行的
package main import( "fmt" "time" ) func main(){ fmt.Println("超時設置") var ch chan string go 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") } }