Tickers are an essential tool for executing a block of code at regular intervals in Golang. Unlike timers, which are used for timeouts, tickers repeat the execution of a task every n seconds. By using a ticker, you can ensure that a specific task runs repeatedly, without having to write repetitive code.

Golang’s goroutines enable concurrent execution of tasks, and tickers can be used in goroutines to take advantage of this feature. By running a ticker inside a goroutine, you can execute a specific task repeatedly at regular intervals while allowing other tasks to run concurrently. This makes tickers a powerful tool for concurrent programming in Golang.

In this article, we’ll take a closer look at how to use tickers in Golang and explore some examples to help you get started. We’ll show you how to create a ticker, how to use it to execute code, and how to customize the interval at which it ticks. Whether you’re a beginner or an experienced Golang developer, understanding tickers is essential to writing efficient and effective code.

Example

time.Tick

You can use the function time.Tick(n). An example call is time.Tick(time.Second * 1). Combined with range that repeats every second.

You can combine it with time.Sleep(n) to make it won’t simply quit while running the goroutine.

package main

import "fmt"
import "time"

func task() {
    for range time.Tick(time.Second *1){
            fmt.Println("Tick ")
    }
}

func main() {
    go task()
    time.Sleep(time.Second * 5)
}

Tickers

You can create a ticker anywhere in the code with the line time.NewTicker(n). Then you can use it to tick every interval:

package main

import "fmt"
import "time"

func task() {
    ticker := time.NewTicker(time.Second * 1)
    for range ticker.C {
        fmt.Println("Tick ")
    }
}

func main() {
    go task()
    time.Sleep(time.Second * 5)    
}

Using Tickers in Golang

Tickers are a useful feature in Golang for executing a block of code at regular intervals. Unlike timers, which are used for timeouts, tickers repeat the execution of a task every n seconds. In this article, we’ll take a closer look at how to use tickers in Golang.

Creating a Ticker

In Golang, we can create a ticker using the time.NewTicker() function. This function takes a duration as its argument and returns a new ticker that will tick at the specified interval. For example, the following code creates a new ticker that ticks every second:

ticker := time.NewTicker(1 * time.Second)

Using a Ticker

Once we have created a ticker, we can use it to execute a block of code at each tick. The most common way to do this is to use a for range loop to iterate over the channel returned by the ticker’s C field. For example, the following code creates a new ticker that ticks every second and prints the message “Tick” to the console on each tick:

ticker := time.NewTicker(1 * time.Second)

for range ticker.C {
    fmt.Println("Tick")
}

We can also stop the ticker when we’re done with it by calling its Stop() method. For example:

ticker := time.NewTicker(1 * time.Second)

for range ticker.C {
    // Do something...
}

ticker.Stop()

Customizing the Interval

We can customize the interval at which the ticker ticks by passing a different duration to time.NewTicker(). For example, the following code creates a new ticker that ticks every 500 milliseconds:

ticker := time.NewTicker(500 * time.Millisecond)

Conclusion

In this article, we’ve learned how to use tickers in Golang to execute a block of code at regular intervals. We’ve seen how to create a ticker, how to use it to execute code, and how to customize the interval at which it ticks. Tickers are a powerful tool in the Golang toolbox, and mastering them can help you write more efficient and effective code.