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 {
	}
}
posted on 2022-05-06 16:09  每天进步一点点点点点  阅读(235)  评论(0编辑  收藏  举报