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

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

问题描述

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

解决方案

所以你需要Interval Timer在每天中午运行一个函数,你可以使用:

time.AfterFunc() time.Tick() time.Sleep() time.Ticker



开始计算启动时间到第一个下一个中午的时间间隔并使用一些等待(例如 time.Sleep 或...),然后使用 24 * time .Hour 间隔为下一个时间间隔。使用 time.Sleep

  package main 

importfmt
importtime

func noonTask() {
fmt.Println(time.Now())
fmt.Println(做一些工作。)
}
func initNoon(){
t:= time.Now()
n:= time.Date(t.Year(),t.Month(),t.Day(),12,0,0,0,t.Location())
d:= n.Sub(t)
如果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更改为此(或您需要的任何东西):

$ pregenNoon()

//在这里执行正常的任务:
$ prefb =lang-golang prettyprint-override>
fmt.Println(在这里做普通任务)
time.Sleep(1 * time.Minute)
}
}
<

使用 time.AfterFunc

package main

import(
fmt
sync
time


func noonTask(){
fmt.Println(time.Now())
fmt.Println(做一些工作。)
time.AfterFunc(duration(),noonTask)
}
func main(){
time.AfterFunc(duration(),noonTask)
wg.Add(1)
//在这里执行正常任务
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

fmt
同步
时间
)$ b $包含主要

b
var ticker * time.Ticker = nil

func noonTask(){
if ticker == nil {
ticker = time.NewTicker(24 * time。小时)
}
用于{
fmt.Println(time.Now())
fmt.Println(做一些工作。)
<-ticker .C
}
}
func main(){
time.AfterFunc(duration(), noonTask)
wg.Add(1)
//在这里执行正常任务
wg.Wait()
}

func duration()time。持续时间{
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 /正在执行的任务


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?

解决方案

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

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.

sample code using 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()
}

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)
    }
}

using time.AfterFunc:

package main

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

func noonTask() {
    fmt.Println(time.Now())
    fmt.Println("do some job.")
    time.AfterFunc(duration(), noonTask)
}
func main() {
    time.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

using 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() {
    time.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  

and see:
https://github.com/jasonlvhit/gocron
Golang - How to execute function at specific times
Golang: Implementing a cron / executing tasks at a specific time

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

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