package main import ( "github.com/robfig/cron" "log" ) func main() { i := 0 c := cron.New() spec :="50 7 * * * ?"// "*/5 * * * * ?" c.AddFunc(spec, func() { i++ log.Println("cron running:", i) }) c.Start() select{} }
任意时间的xx:07:50执行任务
API server listening at: 127.0.0.1:58401
2018/01/09 14:07:50 cron running: 1
spec :="*/5 * * * * ?"
每5秒执行一次任务
API server listening at: 127.0.0.1:58900
2018/01/09 14:11:30 cron running: 1
2018/01/09 14:11:35 cron running: 2
2018/01/09 14:11:40 cron running: 3
2018/01/09 14:11:45 cron running: 4
2018/01/09 14:11:50 cron running: 5
2018/01/09 14:11:55 cron running: 6
2018/01/09 14:12:00 cron running: 7
2018/01/09 14:12:05 cron running: 8
2018/01/09 14:12:10 cron running: 9