Go Cron 表达式的定时任务调度库


package main

import (
	"fmt"
	"log"
	"time"
)
import "gopkg.in/robfig/cron.v3"

//Cron表达式的定时任务调度库 https://godoc.org/gopkg.in/robfig/cron.v3

func main() {
	log.Println("Starting...")

	nyc, _ := time.LoadLocation("Asia/Shanghai")
	//var c = cron.New()  //默认分开始
	var c = cron.New(cron.WithSeconds(), cron.WithLocation(nyc))

	id, e := c.AddFunc("*/2 * * * * *", func() {
		log.Println("schedule every two seconds ...")
	})
	if e != nil {
		log.Fatalln("添加任务失败: " + e.Error())
	}
	log.Println("schedule EntryID: " + fmt.Sprintf("%d", id))

	id2, e := c.AddFunc("@daily", func() {
		fmt.Println("Every day")
	})
	if e != nil {
		log.Fatalln("添加任务失败: " + e.Error())
	}
	log.Println("schedule EntryID: " + fmt.Sprintf("%d", id2))
	c.Start()
	select {}
}

你可能感兴趣的:(Go)