中午在 Golang 中运行代码 [英] Running code at noon in Golang

查看:24
本文介绍了中午在 Golang 中运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以每天中午执行代码?该程序在其剩余的运行时间内处理用户输入,但需要在中午运行一个函数来输出一些文本.最有效的方法是什么?

Is it possible to execute code at a noon everyday? The program is handling user input the rest of its run time but needs to run a function at noon to output some text. What is the most effective way to do this?

推荐答案

所以你需要Interval Timer每天中午运行一个功能,你可以使用:
timer.AfterFunc()time.Tick()time.Sleep()time.Ticker

So you need Interval Timer to run one function at noon everyday, you may use:
timer.AfterFunc() or time.Tick() or time.Sleep() or time.Ticker

首先当程序启动时计算启动时间到下一个中​​午的时间间隔并使用一些等待(例如time.Sleep或...)然后使用24 * time.Hour 下一个间隔的间隔.

first when program starts calculate time interval for start up time till first next noon and use some wait (e.g. time.Sleep or ...) then use 24 * time.Hour interval for the next interval.

使用time.Sleep的示例代码:

package main

import "fmt"
import "time"

func noonTask() {
    fmt.Println(time.Now())
    fmt.Println("do some job.")
}
func initNoon() {
    t := time.Now()
    n := time.Date(t.Year(), t.Month(), t.Day(), 12, 0, 0, 0, t.Location())
    d := n.Sub(t)
    if d < 0 {
        n = n.Add(24 * time.Hour)
        d = n.Sub(t)
    }
    for {
        time.Sleep(d)
        d = 24 * time.Hour
        noonTask()
    }
}
func main() {
    initNoon()
}

并且您可以将 main 更改为此(或您需要的任何内容):

and you may change main to this (or any thing you need):

func main() {
    go initNoon()

    // do normal task here:
    for {
        fmt.Println("do normal task here")
        time.Sleep(1 * time.Minute)
    }
}

使用timer.AfterFunc:

package main

import (
    "fmt"
    "sync"
    "time"
)

func noonTask() {
    fmt.Println(time.Now())
    fmt.Println("do some job.")
    timer.AfterFunc(duration(), noonTask)
}
func main() {
    timer.AfterFunc(duration(), noonTask)
    wg.Add(1)
    // do normal task here
    wg.Wait()
}

func duration() time.Duration {
    t := time.Now()
    n := time.Date(t.Year(), t.Month(), t.Day(), 12, 0, 0, 0, t.Location())
    if t.After(n) {
        n = n.Add(24 * time.Hour)
    }
    d := n.Sub(t)
    return d
}

var wg sync.WaitGroup

使用time.Ticker:

package main

import (
    "fmt"
    "sync"
    "time"
)

var ticker *time.Ticker = nil

func noonTask() {
    if ticker == nil {
        ticker = time.NewTicker(24 * time.Hour)
    }
    for {
        fmt.Println(time.Now())
        fmt.Println("do some job.")
        <-ticker.C
    }
}
func main() {
    timer.AfterFunc(duration(), noonTask)
    wg.Add(1)
    // do normal task here
    wg.Wait()
}

func duration() time.Duration {
    t := time.Now()
    n := time.Date(t.Year(), t.Month(), t.Day(), 12, 0, 0, 0, t.Location())
    if t.After(n) {
        n = n.Add(24 * time.Hour)
    }
    d := n.Sub(t)
    return d
}

var wg sync.WaitGroup  

并查看:
https://github.com/jasonlvhit/gocron
Golang - 如何在特定时间执行函数
Golang:实现cron/在特定时间执行任务

这篇关于中午在 Golang 中运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆