Tickers _ golang

Timers are for when you want to do something once in the future - tickers are for when you want to do something repeatedly at regular intervals. Here'an example of a ticker that ticks periodically until we stop it

package main

import (
    "fmt"
    "time"
)

func main() {

    ticker := time.NewTicker(time.Millisecond * 500)
    go func() {
        for t := range ticker.C {
            fmt.Println("Tick at", t)
        }
    }()

    time.Sleep(time.Millisecond * 1500)
    ticker.Stop()
    fmt.Println("Ticker stopped")
}
Tick at 2015-03-18 13:20:55.55095115 +0800 CST
Tick at 2015-03-18 13:20:56.050762096 +0800 CST
Tick at 2015-03-18 13:20:56.550295916 +0800 CST
Ticker stopped

总结  : 

  1 : ....

本文来自:博客园

感谢作者:jackkiexu

查看原文:Tickers _ golang

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。