Execute the program every 5 minutes with gocron

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)
}

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

回答

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

scheduler := gocron.NewScheduler(time.UTC)
scheduler.Every(5).Minute().Do(hi)
scheduler.StartBlocking()

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

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


以上是Execute the program every 5 minutes with gocron的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>