O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The Ticker holds the C channel which delivers the periodical ticks. The instance is created with a given interval between ticks. The interval is defined by the time.Duration value.  

The code which is intended to be executed periodically is executed in the goroutine in an infinite loop. The reading from the Ticker channel blocks the loop until the tick is delivered.

Note that once the Ticker is stopped by calling the Stop method, the C channel is not closed, it just stops delivering the ticks. For this reason, the preceding code contains the select construct where the stop channel can deliver the stop signal. This way, a graceful shutdown can be done.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required