golang 中的ticker
- 直接就是代码
package main
import (
"fmt"
"time"
)
var Ticker *time.Ticker
func init() {
Ticker = time.NewTicker(time.Second)
}
func Dosomething(c <-chan time.Time) {
for range c {
// do something
fmt.Println(1)
}
// ?????? what's this
fmt.Println("ticker stop")
}
func main() {
go Dosomething(Ticker.C)
time.Sleep(time.Second * 3)
Ticker.Stop()
time.Sleep(time.Second*2)
}
- 输出:
1
1
1
Process finished with exit code 0