Timers and Tickers | Challenge

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This challenge is about using tickers in Golang. Tickers are used when you want to do something repeatedly at regular intervals.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/ConcurrencyGroup(["`Concurrency`"]) go/ConcurrencyGroup -.-> go/tickers("`Tickers`") subgraph Lab Skills go/tickers -.-> lab-15440{{"`Timers and Tickers | Challenge`"}} end

Timers and Tickers

In this challenge, you need to create a ticker that ticks every 500ms until we stop it. You will use a channel to await the values as they arrive.

Requirements

  • Use the time package to create a ticker.
  • Use a channel to await the values as they arrive.
  • Use the select statement to receive values from the channel.
  • Stop the ticker after 1600ms.

Example

## When we run this program the ticker should tick 3 times
## before 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

Summary

In this challenge, you learned how to use tickers in Golang. You created a ticker that ticks every 500ms until we stop it, used a channel to await the values as they arrive, and stopped the ticker after 1600ms.

Other Go Tutorials you may like