【发布时间】:2016-05-04 08:42:18
【问题描述】:
为什么这个 Golang 代码不能在多个时间中进行选择。在频道工作后?
见下面的代码。永远不会发出“超时”消息。为什么?
package main
import (
"fmt"
"time"
)
func main() {
count := 0
for {
select {
case <-time.After(1 * time.Second):
count++
fmt.Printf("tick %d\n", count)
if count >= 5 {
fmt.Printf("ugh\n")
return
}
case <-time.After(3 * time.Second):
fmt.Printf("timeout\n")
return
}
}
}
在 Playground 上运行它:http://play.golang.org/p/1gku-CWVAh
输出:
tick 1
tick 2
tick 3
tick 4
tick 5
ugh
【问题讨论】: