In golang, Stop cannot be called to stop ticker after the pointer returned by time.NewTicker () is valued.

  golang, question


As shown in the figure, the code is not long, Stop does not respond to Ticker pointer values in tickerTest1, and tickerTest2 does not have any problems directly using the returned pointer.

I turned over the runtime package and found that there was such a judgment when I stopped:

if i < 0 || i > last || tb.t[i] !  = t {
 return false

The key is …tb.t[i] ! = t. Such as@ Li YiSaid, if you usetick := *time.NewTicker(time.Second)If a copy is made, this condition must be true, that is, not releasing the timer resource is equivalent to no stop. So the timer did not stop.

What everyone needs to pay attention to is:Don’t use range for Ticker.C, because the channel will not be close when Ticker.Stop is called, so this is equivalent to a dead loop.