dongying195959 2016-07-15 01:57
浏览 23
已采纳

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?

  • 写回答

1条回答 默认 最新

  • duanbin198788 2016-07-15 03:08
    关注

    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

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

    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() {
        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  
    

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog