May 28, 2023

Is There A Way To Do Repetitive Tasks At Intervals?

The function time.NewTicker makes a channel that sends a periodic message, and provides a way to stop it. Use it something like this (untested):

ticker := time.NewTicker(5 * time.Second)
quit := make(chan struct{})
go func() {
    for {
       select {
        case <- ticker.C:
            // do stuff
        case <- quit:

You can stop the worker by closing the quit channel: close(quit).

Leave a Reply