介绍
goCron is a Golang job scheduling package which lets you run Go functions periodically at pre-determined interval using a simple, human-friendly syntax.
使用 安装 1 go get github.com/go-co-op/gocron
示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 package mainimport ( "github.com/go-co-op/gocron" "log" "time" ) func main () { s := gocron.NewScheduler(time.Local) log.Println("start" ) s.Every(2 ).Seconds().Do(func () { log.Println("执行了。。。。2s" ) }) s.Every(5 ).Seconds().Do(func () { log.Println("啦啦啦111" ) }) s.Every(5 ).Seconds().WaitForSchedule().Do(func () { log.Println("啦啦啦222" ) }) s.Cron("*/1 * * * *" ).Do(task) s.CronWithSeconds("*/1 * * * * *" ).Do(task) s.Every(1 ).Sunday().At("00:30" ).Do(task) s.Every(1 ).Day().At("10:00" ).Do(task) s.Every("2" ).Seconds().SingletonMode().Do(task) s.Every(1 ).Seconds().Do(taskWithParams, 2 , "test" ) s.StartAsync() s.StartBlocking() } func task () { log.Println("hello" ) } func taskWithParams (a int , b string ) { log.Println(a, b) }