as shown in the figure, the code is not long, Stop does not respond to the value of the Ticker pointer in tickerTest1, and there is no problem for tickerTest2 to use the returned pointer directly.
as shown in the figure, the code is not long, Stop does not respond to the value of the Ticker pointer in tickerTest1, and there is no problem for tickerTest2 to use the returned pointer directly.
rummaged through the runtime package and found that there was such a judgment in stop:
if i < 0 || i > last || tb.t[i] != t {
return false
}
the key lies in tb.t [I]! = t
. As @ Li Yi said, if a copy is copied using tick: = * time.NewTicker (time.Second)
, this condition must be true, that is, if the timer resource is not released, it is equivalent to no stop. So the timer didn't stop.
what everyone needs to note is that do not use range, for Ticker.C because the channel, is not close when Ticker.Stop is called, so it is equivalent to an endless loop.
v2 = * p1
produces a new structure v2 , whose value is copied from the structure referred to by the p1 pointer.
therefore, the ticker.Stop () that you call in tickerTest2 () is not the one created by time.NewTicker () , but a copy.
give an example and refer to
package main
import (
"testing"
)
func Test_dereference_pointer(t *testing.T) {
type Foo struct {
N int
}
p1 := &Foo{1}
f2 := *p1 // p1 f2
p1.N = 2
f2.N = 3
if p1.N != 2 || f2.N != 3 {
t.Fatal("error")
}
}