Execute the program every 5 minutes with gocron

-4 go golang-migrate go-modules

I need to run my Co program continuously with five minute interval.

I tried using gocron but the program is not giving any output.

func hi() {
    fmt.Println("hi")
}
func main() {
    gocron.Every(5).Minute().Do(hi)
}
Run Code Online (Sandbox Code Playgroud)

I expect this to run and print "hi" at every 5 min interval.

Fen*_*til 5

您的代码只是设置了规则并立即退出。您必须启动将运行分配的作业的调度程序。

scheduler := gocron.NewScheduler(time.UTC)
scheduler.Every(5).Minute().Do(hi)
scheduler.StartBlocking()
Run Code Online (Sandbox Code Playgroud)

这样调度程序将阻塞程序直到它停止(例如通过 Ctrl-C)。

有关更多信息,请参阅文档