Golang定时任务简单实现

sunchaotongylz 20 0 PDF 2021-01-16 21:01:17

下载cron包: go get github.com/robfig/cron 开启一个定时: 根据cron表达式进行时间调度,cron可以精确到秒,大部分表达式格式也是从秒开始。 c := cron.New()默认从分开始,所以加上cron.WithSeconds(),保证定时按照表达式字面意思执行。 func main() { c := cron.New(cron.WithSeconds()) //精确到秒 //定时任务 spec := */1 * * * * ? //cron表达式,每秒一次 c.AddFunc(spec, func() { fmt.Prin

用户评论
请输入评论内容
评分:
暂无评论