// 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") } }

 

相关文章:

  • 2022-12-23
  • 2023-03-20
  • 2021-05-19
  • 2021-09-24
  • 2021-05-25
  • 2021-04-03
  • 2022-02-11
猜你喜欢
  • 2021-11-29
  • 2022-12-23
  • 2021-10-22
  • 2021-11-23
  • 2022-01-09
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案