我如何在 Golang 中使用 'time.After' 和 'default'?

How can I use 'time.After' and 'default' in Golang?

我正在尝试理解一段简单的Golang例程代码:

package main

import (
    "fmt"
    "time"
)

func sleep(seconds int, endSignal chan<- bool) {
    time.Sleep(time.Duration(seconds) * time.Second)
    endSignal <- true
}

func main() {
    endSignal := make(chan bool, 1)
    go sleep(3, endSignal)
    var end bool

    for !end {
        select {
        case end = <-endSignal:
            fmt.Println("The end!")
        case <-time.After(5 * time.Second):
            fmt.Println("There's no more time to this. Exiting!")
            end = true
        }
    }

}

很好,但为什么我不能在此 "select" 块中使用简单的默认值?像这样:

for !end {
    select {
    case end = <-endSignal:
        fmt.Println("The end.")
    case <-time.After(4 * time.Second):
        fmt.Println("There's no more time to this. Exiting!")
        end = true
    default:
        fmt.Println("No end signal received.")
    }
}

它得到这个输出:

❯ go run goroutines-timeout.go
No end signal received!
No end signal received!
No end signal received!
No end signal received!
...
The end!

我不明白为什么。

每次执行 time.After(4 * time.Second) 都会创建一个新的计时器通道。 select 语句无法记住它在上一次迭代中 select 编辑的频道。您还采用了异步操作并将其变成一个繁忙的循环,从而违背了 select 语句的目的。

您只需要一个简单的 select 围绕您感兴趣的两个频道。它根本不需要循环。

select {
case <-endSignal:
    fmt.Println("The end!")
case <-time.After(4 * time.Second):
    fmt.Println("There's no more time to this. Exiting!")
}

https://play.golang.org/p/jb4EE8e6cw

如果你真的想多次轮询,把定时器放在for循环之外,这样每次迭代都会检查同一个

timeout := time.After(5 * time.Second)
pollInt := time.Second

for {
    select {
    case <-endSignal:
        fmt.Println("The end!")
        return
    case <-timeout:
        fmt.Println("There's no more time to this. Exiting!")
        return
    default:
        fmt.Println("still waiting")
    }
    time.Sleep(pollInt)
}