This content originally appeared on Level Up Coding - Medium and was authored by Radhakishan Surwase
Timers and Tickers let you write code that executes in the future, once or repeatedly.
In this tutorial, we are going through timers & tickers in Go and how you can use them effectively within your own Go applications.
Timers vs Tickers
Timers — These are used for one-off tasks. It represents a single event in the future. You tell the timer how long you want to wait, and it provides a channel that will be notified at that time.
Tickers — Tickers are exceptionally helpful when you need to perform an action repeatedly at given time intervals. We can use tickers, in combination with goroutines to run these tasks in the background of our applications.
A Simple Timer
Let’s start off with a really simple timer in which will run after 2 seconds. The execution of time is associated with a goroutine. The purpose of using done is to control the execution of the program only.
Stop Timer before laps
In the below code listing, you can make a call to the Stop() method on the timer to stop its execution. We have configured a time to be executed after 10 seconds but line no.14 stopped it prematurely.
A Simple Ticker
Let’s start off with a really simple in which we repeatedly run a simple fmt.Println statement every 1seconds.
Stopping A Ticker
In the code listing below a ticker is configured to be executed after every 1 second. A goroutine is written to monitor if a ticker event is received or done channel got a signal to stop it. Before sending a signal to the done channel, at line 24, we have made a call to time.Sleep(10*time. Second) to let ticker execute 10 times.
After sleeping for 10 seconds, the statement at line no. 25 will be executed which results in stopping the ticker.
A Background Ticker
If we had a task that we wanted to run in the background, we could move our for a loop that iterates over ticker.C to inside a goroutine which will allow our application to execute other tasks.
Let’s move the code for creating the ticker and looping it into a new function called bgTask() and then, within our main() function, we’ll call this as a goroutine using the go keyword like so
Conclusion
We often want to execute the Go code at some point in the future, or repeatedly at some interval. Go’s the built-in timer and ticker features make both of these tasks easy. We can use these implementations in a much-controlled way.
That's all for now…. Keep Learning ….. Happy Learning ?
Timer and Ticker Using Golang was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Radhakishan Surwase
Radhakishan Surwase | Sciencx (2021-07-05T15:20:00+00:00) Timer and Ticker Using Golang. Retrieved from https://www.scien.cx/2021/07/05/timer-and-ticker-using-golang/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.