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

Jon*_*ber 7 go goroutine

我试图理解一个简单的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
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这很好,但为什么我不能在这个"选择"块中使用简单的默认值?像这样的东西:

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.")
    }
}
Run Code Online (Sandbox Code Playgroud)

它得到这个输出:

? go run goroutines-timeout.go
No end signal received!
No end signal received!
No end signal received!
No end signal received!
...
The end!
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么.

Jim*_*imB 16

每次执行时time.After(4 * time.Second)都会创建一个新的计时器通道.有没有办法的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!")
}
Run Code Online (Sandbox Code Playgroud)

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)
}
Run Code Online (Sandbox Code Playgroud)