lean into examples
This commit is contained in:
31
examples/tickers/tickers.go
Normal file
31
examples/tickers/tickers.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// [Timers](timers) are for when you want to do
|
||||
// something once in the future - tickers are for when you
|
||||
// want to do something repeatedly at regular intervals.
|
||||
// Here's an example of a ticker that ticks periodically
|
||||
// until we stop it.
|
||||
|
||||
package main
|
||||
|
||||
import "time"
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
||||
// Tickers use a similar mechanism to timers: a
|
||||
// chanel that is sent values. Here we'll use the
|
||||
// `range` builtin on the channel to iterate over
|
||||
// the values as they arrive every 500ms.
|
||||
ticker := time.NewTicker(time.Millisecond * 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 won't receive any more values on its
|
||||
// channel. We'll stop ours after 1500ms.
|
||||
time.Sleep(time.Millisecond * 1500)
|
||||
ticker.Stop()
|
||||
fmt.Println("Ticker stopped")
|
||||
}
|
||||
7
examples/tickers/tickers.sh
Normal file
7
examples/tickers/tickers.sh
Normal file
@@ -0,0 +1,7 @@
|
||||
# When we run this program the ticker should tick 3 times
|
||||
# befor we stop it.
|
||||
$ go run tickers.go
|
||||
Tick at 2012-09-23 11:29:56.487625 -0700 PDT
|
||||
Tick at 2012-09-23 11:29:56.988063 -0700 PDT
|
||||
Tick at 2012-09-23 11:29:57.488076 -0700 PDT
|
||||
Ticker stopped
|
||||
Reference in New Issue
Block a user