-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tickers.go
35 lines (31 loc) · 763 Bytes
/
Tickers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Tickers
package main
import (
"fmt"
"time"
)
func Tickers_() {
/*
tickers are for when you want to
do something repeatedly at regular intervals.
Tickers use a similar mechanism to timers:
a channel that is sent values.
Here we will use the range builtin on the channel
to iterate over the values as they arrive every 500ms.
*/
ticker := time.NewTicker(time.Microsecond * 500)
go func() {
for t := range ticker.C {
fmt.Println("Tick at ", t)
}
}()
/*
Tickers can be stopped like timers.
Once a ticker is stopped it would not receive
any more values on its channel.
We will stop ours after 1500ms.
*/
time.Sleep(time.Microsecond * 1500)
ticker.Stop()
fmt.Println("Ticker stopped.")
}