执行上代码
package main
import (
"gopkg.in/robfig/cron.v2"
"sync"
"fmt"
"time"
)
func main() {
//doTime 秒 分 时 日 月 星期
doTime := time.Now().Add(10*time.Second).Format("05 04 15 * * *")
fmt.Printf(doTime)
cron := cron.New()
cron.Start()
id, _ := cron.AddFunc(doTime, func() {
fmt.Println("======>>run the cron")
//wg.Done()
}, 1)
fmt.Println("======>>", id)
fmt.Println("10秒之后执行定时任务", id)
//删除定时任务
//cron.Remove(id)
//停止定时任务
//defer cron.Stop()
time.Sleep(2 * time.Hour)
}
执行结果
➜ test go run cron.go
00 06 09 * * *======>> 1
10秒之后执行定时任务 1
======>>run the cron