Timer and Ticker Using Golang

Timers and Tickers let you write code that executes in the future, once or repeatedly.

Photo by Veri Ivanova on Unsplash

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.

https://medium.com/media/057c03fab153121584c32efd2f34f367/href

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.

https://medium.com/media/477e9a21d46361d327344ac561e08618/href

A Simple Ticker

Let’s start off with a really simple in which we repeatedly run a simple fmt.Println statement every 1seconds.

https://medium.com/media/d02193f455fca363e019619d2982cb1b/href

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.

https://medium.com/media/66ba6f4e5db0ef59f22baebc3fac0c93/href

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

https://medium.com/media/cc083793607902a675e765c6d6423d93/href

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

Timers and Tickers let you write code that executes in the future, once or repeatedly.

Photo by Veri Ivanova on Unsplash

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Timer and Ticker Using Golang." Radhakishan Surwase | Sciencx - Monday July 5, 2021, https://www.scien.cx/2021/07/05/timer-and-ticker-using-golang/
HARVARD
Radhakishan Surwase | Sciencx Monday July 5, 2021 » Timer and Ticker Using Golang., viewed ,<https://www.scien.cx/2021/07/05/timer-and-ticker-using-golang/>
VANCOUVER
Radhakishan Surwase | Sciencx - » Timer and Ticker Using Golang. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/05/timer-and-ticker-using-golang/
CHICAGO
" » Timer and Ticker Using Golang." Radhakishan Surwase | Sciencx - Accessed . https://www.scien.cx/2021/07/05/timer-and-ticker-using-golang/
IEEE
" » Timer and Ticker Using Golang." Radhakishan Surwase | Sciencx [Online]. Available: https://www.scien.cx/2021/07/05/timer-and-ticker-using-golang/. [Accessed: ]
rf:citation
» Timer and Ticker Using Golang | Radhakishan Surwase | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.