go cron 定时任务
1、安装依赖包
go get github.com/robfig/cron
2、多个定时任务
type TestJob1 struct { }func (this TestJob1) Run() {
fmt.Println("this is job1!!!!!")
}type TestJob2 struct {
}func (this TestJob2) Run() {
fmt.Println("this is job2!!!!!!!")
}
func main(){ c := cron.New() spec := "*/5 * * * * *" //cron周期 秒 分 时 日 月 星期 //添加job c.AddJob(spec, TestJob1{}) c.AddJob(spec, TestJob2{}) //启动定时任务 c.Start() //关闭定时任务 defer c.Stop() for { } }