I know the Stop
function can not close the channel. I am just confused with the two different results of tickerTest1
and tickerTest2
.
package main
import (
"time"
"log"
)
func tickerTest1() {
ticker := *time.NewTicker(time.Second)
count := 0
go func() {
time.Sleep(3*time.Second)
ticker.Stop()
}()
for range ticker.C {
count ++
log.Println("tickerTest1:", count)
}
}
func tickerTest2() {
ticker := time.NewTicker(time.Second)
count := 0
go func() {
time.Sleep(3*time.Second)
ticker.Stop()
}()
for range ticker.C {
count ++
log.Println("tickerTest2:", count)
}
}
func main() {
go tickerTest1()
tickerTest2()
}