go

Go time.Tick vs time.NewTicker


I am new to Go and started learning recently. I have come across tickers and timers. In the tickers, we can create a ticker in two ways

throttler := time.Tick(time.Millisecond * 50)
<- throttler

throttler := time.NewTicker(time.Millisecond * 500)
<- throttler.C

What is the difference between them other than syntax?


Solution

  • From the time.Tick documentation:

    Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. Unlike NewTicker, Tick will return nil if d <= 0.