Skip to content

Timer

https://gobyexample-cn.github.io/timers

go
   timer1 := time.NewTimer(2 * time.Second)

    <-timer1.C // 会卡主,2秒后收到值
    fmt.Println("Timer 1 fired")
go
    timer2 := time.NewTimer(time.Second)

    go func() {
        <-timer2.C
        fmt.Println("Timer 2 fired")
    }()

    // 因为立即 stop 所以 “Timer 2 fired” 并不会被执行
    stop2 := timer2.Stop()

    if stop2 {
        fmt.Println("Timer 2 stopped")
    }

    time.Sleep(2 * time.Second)