time.Sleep居然会失效???求原理???

elitah · · 2732 次点击
你可以试试,看打印出什么 ```golang package main import ( "fmt" "time" ) var c = make(chan int, 1) func main() { go aaa() sec := 0 for { select { case <-c: fmt.Println("after 3 sec") return case <-time.After(time.Second): sec = sec + 1 fmt.Println(sec) } } } func aaa() { time.Sleep(time.Second * 3) c <- 1 } ```
#4
更多评论
主机是linux(ubuntu 14.04、ubuntu 16.06都试过) 系统都是386 ![捕获.PNG](https://static.studygolang.com/180330/22fdbfabaeddb84a45b13ac1fde2601b.PNG)
#1
```golang func main() { go aaa() for { select { case <-ch: //拿到锁 fmt.Println("call") case <-time.After(5 * time.Second): //超时5s fmt.Println("5 sec call") } } } ```
#2