Is there any difference between
ticker := time.NewTicker(1 * time.Second)
go func() {
for _ = range ticker.C {
fmt.Print("Tick")
}
}()
time.Sleep(3)
ticker.Stop()
and
var ticker *time.Ticker
go func() {
ticker = time.NewTicker(1 * time.Second)
for _ = range ticker.C {
fmt.Print("Tick")
}
}()
time.Sleep(3)
ticker.Stop()
in terms of thread-safeness, especially when the work in the function takes longer than a tick's period?
I am asking because (when stopping the Ticker is not required) the latter can be shortened to
go func() {
for ticker := time.NewTicker(1 * time.Second) ;; <-ticker.C {
fmt.Print("Tick")
}
}()
while the former cannot. This form has the additional advantage that the first tick is triggered right away.