From 5bb6b01f89e342f42c5ff4a786663885e965fa3f Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Tue, 9 Oct 2012 11:40:40 -0700 Subject: [PATCH] tickers --- src/tickers/tickers.go | 17 +++++++++++++++-- src/tickers/tickers.sh | 2 ++ src/timers/timers.go | 8 +++----- src/timers/timers.sh | 3 +++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/tickers/tickers.go b/src/tickers/tickers.go index 4e6fc5c..1d9d5bc 100644 --- a/src/tickers/tickers.go +++ b/src/tickers/tickers.go @@ -1,20 +1,33 @@ // ## Tickers +// [Timers](timers.html) 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") } - -// todo: pull from blog post diff --git a/src/tickers/tickers.sh b/src/tickers/tickers.sh index 6a2ce47..bdbb3a3 100644 --- a/src/tickers/tickers.sh +++ b/src/tickers/tickers.sh @@ -1,3 +1,5 @@ +# 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 diff --git a/src/timers/timers.go b/src/timers/timers.go index 29f815c..826c9bf 100644 --- a/src/timers/timers.go +++ b/src/timers/timers.go @@ -3,7 +3,8 @@ // We often want to execute Go code at some point in the // future, or repeatedly at some interval. Go's built-in // timer and ticker features make both of these tasks -// easy. First we'll look at timers. +// easy. We'll look first at timers and then +// at [tickers](tickers.html). package main @@ -27,15 +28,12 @@ func main() { // If you just wanted to wait, you could have used // `time.Sleep`. One reason a timer may be useful is // that you can cancel the timer before it expires. + // Here's an example of that. timer2 := time.NewTimer(time.Second) go func() { <-timer2.C fmt.Println("Timer 2 expired") }() - - // In this case we'll cancel the timer before it has a - // change to expire (`.Stop()` would return `false` if - // we tried to cancel it after it expired). stop2 := timer2.Stop() if stop2 { fmt.Println("Timer 2 stopped") diff --git a/src/timers/timers.sh b/src/timers/timers.sh index 79f462d..b4963a9 100644 --- a/src/timers/timers.sh +++ b/src/timers/timers.sh @@ -1,3 +1,6 @@ +// The first timer will expire ~2s after we start the +// program, but the second should be stopped before it has +// a chance to expire. $ go run timers.go Timer 1 expired Timer 2 stopped