亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在每天的特定時間運行作業?

如何在每天的特定時間運行作業?

Go
慕田峪4524236 2022-08-15 10:11:43
我想每天晚上9點打印出來。如何在 Go 中執行此操作?do my job以下是我到目前為止所得到的:timer := time.NewTimer(3 * time.Second)for {    now := time.Now()    next := now.Add(time.Hour * 24)    todayNine := time.Date(next.Year(), next.Month(), next.Day(), 9, 0, 0, 0, next.Location()).AddDate(0, 0, -1)    todayFifteen := time.Date(next.Year(), next.Month(), next.Day(), 15, 0, 0, 0, next.Location()).AddDate(0, 0, -1)    todayEnd := time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location()).AddDate(0, 0,  -1)    if now.Before(todayNine) {        timer.Reset(todayNine.Sub(now))    } else if now.Before(todayFifteen) {        timer.Reset(todayFifteen.Sub(now))    } else if now.Before(todayEnd) {        timer.Reset(todayEnd.Sub(now))    }    <- timer.C    fmt.Println("do my job")}
查看完整描述

1 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

我會使用包。https://pkg.go.dev/github.com/robfig/croncron


文檔中的示例:


c := cron.New()

c.AddFunc("0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })

c.AddFunc("@hourly",      func() { fmt.Println("Every hour") })

c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty") })

c.Start()

..

// Funcs are invoked in their own goroutine, asynchronously.

...

// Funcs may also be added to a running Cron

c.AddFunc("@daily", func() { fmt.Println("Every day") })

..

// Inspect the cron job entries' next and previous run times.

inspect(c.Entries())

..

c.Stop()  // Stop the scheduler (does not stop any jobs already running).


查看完整回答
反對 回復 2022-08-15
  • 1 回答
  • 0 關注
  • 91 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號