请教个select...case...问题,烦恼了好几天了

orthoc · · 962 次点击
czyt
云在青天水在瓶
对于chan的timeout 参考下面的例子 ```go package main import ( "fmt" "os" "time" ) func main() { notify := make(chan int64, 1) timeoutCnt := 0 go func() { counter := 0 for { time.Sleep(1 * time.Second) notify <- time.Now().UnixNano() counter++ if counter > 30 { break } } }() for { select { case currentTime := <-notify: fmt.Println(currentTime) case <-time.After(5 * time.Second): fmt.Println("time out") timeoutCnt++ if timeoutCnt > 3 { os.Exit(0) } } } } ```
#1