Is there a way to do repetitive background tasks in Go? I\'m thinking of something like Timer.schedule(task, delay, period) in Java. I know I can do this with
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:
ticker.Stop()
return
}
}
}()
You can stop the worker by closing the quit channel: close(quit).