-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33_tickers.go
More file actions
29 lines (25 loc) · 803 Bytes
/
Copy path33_tickers.go
File metadata and controls
29 lines (25 loc) · 803 Bytes
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
package gobyexample
import (
"fmt"
"time"
)
// Timers are for when you want to something once in the future.
// Tickers are for when you want to do something repeatedly at
// regular intervals.
// TickersDemo - demonstrates using Go's built-in tickers.
func TickersDemo() {
// Tickers use a similar mchanism to timers: a channel that is sent values.
// Here we'll use the `range` built-in on the channel to iterate over the
// values as they arrive every 500ms.
ticker := time.NewTicker(500 * time.Millisecond)
go func() {
for t := range ticker.C {
fmt.Println("Tick at", t)
}
}()
// Tickers can be stopped like timers. Once a ticker is stopped it won't receive
// any more values on its channel.
time.Sleep(1600 * time.Millisecond)
ticker.Stop()
fmt.Println("Ticker stopped")
}