Updated April 13, 2023
Introduction to Golang Ticker
In general many time we face a situation where we required to running of time ticker which keeps showing us current time or keeps executing certain peace of code on the given interval of time, in such type of situations we should use the Ticker, to use this we need to use the time package of the go language, we have a method called NewTicker() which allow us to stop and start the time tickers, we needed to create a ticker channel by passing chan and bool as the argument which will be used to check if it is open, if the channel is open means ticker will continue.
Syntax of Golang Ticker
Below is the simple syntax for the ticker in the go language, we explain the below syntax in the following steps.
- time: The time is a tribute of the ticker which we need to import first to our code , as every work related to the ticker is going to happen on this attribute.
- NewTicker(): This is one of the key players in the go language for performing any operation like stopping ticker. This method take expression of time as the arguments, like 2*time. Second or 2*time.Millisecond.Here instead of taking 2 as the number, we can take any number and Second and Millisecond according to our uses.
- Finally, we can call any activity on the variable created by the NewTicker function with the time expression.For example, in the below syntax we have used ticker ticker.stop() to stop time ticker.We can use this with a certain condition where we can check if the time boud given by you has completed if you defined that after 10 seconds the ticker should stop then you can check the time and once 10 second happens we you can call the tickerTicker.stop().
Please see the below syntax for a better understanding.
TimeTicker := time.NewTicker(pass expression containing for time ex 2*time.Second or 2*time.Millisecond)
tickerTicker.Stop()
How Does Ticker Work in Go language?
Before going to explain working we need to understand the uses. Let me give you one real-time example. Suppose you have an application where you want to see the data changes on the server and fetch them for the client, it is possible in case of a trading application where each second trade value get change so in such type of situation we need to get updated trade value. With the help of thicker, it will execute the peace of code on the given time interval and hence updated value will be available for the client.
Working of the Ticker in the go language can be explained with help of the below points,
- First, we need to have the time package of the go language for working with a time ticker.
- Second, think we need to use the function called NewTicker on the time package, and we need to pass the expression of the time to this method. The expression of the time will be like n*time.Second. Here n can be any numeric value like 1,2,3,4,5,6, etc, and time. Second, we are instructing about the time how much it needs to spend. Finally NewTicker() output will be captured in a variable.
- Finally, the variable output return from the function NewTicker() , will be used to perform operations like stopping of the ticker.
- Once we run the code it will start executing the code on each given interval.
- We needed to create a channel with the help of the function makes. The function make will take two arguments like chan and bool. Here bool means boolean type.
- Whatever return from make function in the form of the channel will be checked each time, on the basis of the true or false of the condition it will either call ticker code or return back and stop executions.
Examples to Implement Golang Ticker
Below are some of the important examples which elaborate on the working of the ticker in the go language. In case if you want to execute the below code of examples then create a file with name ticker.go(you can create a file with any name), and copy and paste the below code on the file and run the command go run ticker.go and you can get the output.
Example #1
Below is a simple example where we have taken the NewTicker() function with time as the second. We are mainly doing three things here in the below example they are.
- First, we have created the NewTicker() function by passing the time expression as the argument.
- Finally, we have created a channel by passing the two arguments to then like chan and bool. Here bool is boolean.
- Third, we are checking conditions in cases where we are checking the case trickerChannel. Here the tickerChannel is the variable derive from the make function.
Please see the below example.
Code:
package main
import "fmt"
import "time"
func main() {
TimeTicker := time.NewTicker(3 * time.Second)
tickerChannel := make(chan bool)
go func() {
for {
select {
case timeticker := <-TimeTicker.C:
fmt.Println("The time for current is : ", timeticker)
case <-tickerChannel:
return
}
}
}()
time.Sleep(6 * time.Second)
TimeTicker.Stop()
tickerChannel <- true
fmt.Println("Time for running ticker is completed")
}
Output:
Example #2
This example is similar to the previous example. The only difference is here we have used Millisecond instead of using second as the previous one.
Please see the below example.
Code:
package main
import (
"fmt"
"time"
)
func main() {
tm :=time.Millisecond
tickerTicker := time.NewTicker(400 * tm)
tickerChaneel := make(chan bool)
go func() {
for {
select {
case <-tickerChaneel:
return
case tmtr := <-tickerTicker.C:
fmt.Println("Ticker time at current is", tmtr)
}
}
}()
time.Sleep(1400 * time.Millisecond)
tickerTicker.Stop()
tickerChaneel <- true
fmt.Println("Ticker has stopped now")
}
Output:
Conclusion
From the above example we learned the basic concept of the go ticker, we learned its working and we focus on the syntax and uses of the ticker in go language. We also worked with some of the important examples of the ticker in the go language.
Recommended Article
This is a guide to Golang Ticker. Here we discuss the introduction, syntax, and working of Golang Ticker along with examples and code implementation. You can also go through our other suggested articles to learn more –