|
|
package main |
|
|
|
import ( |
|
"fmt" |
|
"time" |
|
) |
|
|
|
func main() { |
|
|
|
ticker := time.NewTicker(500 * time.Millisecond) |
|
done := make(chan bool) |
|
|
|
go func() { |
|
for { |
|
select { |
|
case <-done: |
|
return |
|
case t := <-ticker.C: |
|
fmt.Println("Tick at", t) |
|
} |
|
} |
|
}() |
|
|
|
time.Sleep(1600 * time.Millisecond) |
Tick at 2020-10-21 16:49:16.221050216 +0000 UTC m=+0.500221756
Tick at 2020-10-21 16:49:16.721060817 +0000 UTC m=+1.000232379
Tick at 2020-10-21 16:49:17.22112193 +0000 UTC m=+1.500293464
|
ticker.Stop() |
Tick at 2020-10-21 16:49:18.057000663 +0000 UTC m=+0.500209920
Tick at 2020-10-21 16:49:18.557044531 +0000 UTC m=+1.000253788
Tick at 2020-10-21 16:49:19.057070021 +0000 UTC m=+1.500279270
|
done <- true |
Tick at 2020-10-21 16:49:19.910618706 +0000 UTC m=+0.500249033
Tick at 2020-10-21 16:49:20.410621395 +0000 UTC m=+1.000251735
Tick at 2020-10-21 16:49:20.910741264 +0000 UTC m=+1.500371590
|
fmt.Println("Ticker stopped") |
Tick at 2020-10-21 16:49:21.736548374 +0000 UTC m=+0.500250576
Tick at 2020-10-21 16:49:22.236535084 +0000 UTC m=+1.000237307
Tick at 2020-10-21 16:49:22.736562531 +0000 UTC m=+1.500264739
Ticker stopped
|
} |
Tick at 2020-10-21 16:49:23.554917688 +0000 UTC m=+0.500245535
Tick at 2020-10-21 16:49:24.054957087 +0000 UTC m=+1.000284919
Tick at 2020-10-21 16:49:24.554927622 +0000 UTC m=+1.500255482
|