您的位置 首页 golang

golang 定时任务

前提:github.com/robfig/cron

    package mainimport (    "github.com/robfig/cron"    "log"    "fmt")type TestJob struct {}func (this TestJob)Run() {    fmt.Println("testJob1...")}type Test2Job struct {}func (this Test2Job)Run() {    fmt.Println("testJob2...")}//启动多个任务func main() {    i := 0    c := cron.New()    //AddFunc    spec := "*/5 * * * * ?"    c.AddFunc(spec, func() {        i++        log.Println("cron running:", i)    })    //AddJob方法    c.AddJob(spec, TestJob{})    c.AddJob(spec, Test2Job{})    //启动计划任务    c.Start()    //关闭着计划任务, 但是不能关闭已经在执行中的任务.    defer c.Stop()    select{}}

文章来源:智云一二三科技

文章标题:golang 定时任务

文章地址:https://www.zhihuclub.com/6697.shtml

关于作者: 智云科技

热门文章

网站地图