Golang Timer and Ticker

GoGoBeginner
Practice Now

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

Introduction

This challenge focuses on the use of timers and tickers in Golang. Timers and tickers are useful for executing code at a specific time or repeatedly at a given interval.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/ConcurrencyGroup(["`Concurrency`"]) go/ConcurrencyGroup -.-> go/timers("`Timers`") subgraph Lab Skills go/timers -.-> lab-15444{{"`Golang Timer and Ticker`"}} end

Timers

The challenge requires the implementation of a timer that waits for a specified duration and then fires. Additionally, the timer should be cancellable before it fires.

Requirements

  • The time package should be imported.
  • Two timers should be created, one that waits for 2 seconds and another that waits for 1 second.
  • The first timer should print "Timer 1 fired" when it fires.
  • The second timer should print "Timer 2 fired" when it fires.
  • The second timer should be cancelled before it fires.
  • The program should wait for 2 seconds to show that the second timer did not fire.

Example

// The first timer will fire ~2s after we start the
// program, but the second should be stopped before it has
// a chance to fire.
$ go run timers.go
Timer 1 fired
Timer 2 stopped

Summary

This challenge demonstrated the use of timers in Golang. Timers can be used to execute code at a specific time or to wait for a specified duration before executing code. Additionally, timers can be cancelled before they fire.

Other Go Tutorials you may like