Calling Stop after the pointer returned by time.NewTicker () in golang cannot stop ticker

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.

Mar.14,2021

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.

< hr >

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")
    }
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e89aa9-f79.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e89aa9-f79.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?