golang 定时任务demo

执行上代码

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值