go - 使用默认时间Golang调用func

标签 go time

我的功能开始。然后2分钟后再次运行。如何在凌晨3点再次运行功能?
我占用时间。

func doEvery(d time.Duration, f func(time.Time)) {
    for x := range time.Tick(d) {
        f(x)
    }
}
func RunUpdateTimeTable(respond chan<- string) {
    respond <- "Start get timetables info service."
    updateTimeTables(time.Now())
    doEvery(2*time.Minute, updateTimeTables)
}

最佳答案

这里是

func doAtEvery3am(f func(time.Time)) {
    for {
        // compute duration until 3am local time
        t := time.Now()
        t = time.Date(t.Year(), t.Month(), t.Day(), 3, 0, 0, 0, time.Local)
        d := time.Until(t)
        if d <= 0 {
            d = time.Until(t.Add(24*time.Hour))
        }
        // wait duration and call f
        f(<-time.After(d))
    }
}

注意:执行f后,我们将持续时间重新计算为凌晨3点,因为f的执行持续时间未知。

关于go - 使用默认时间Golang调用func,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59404062/

相关文章:

powershell - 使用 PowerShell 获取 IIS 的具体回收时间

firebase - 在 Firestore 中忽略 Nil 值的最佳实践

io - Golang io.copy 在请求正文上两次

java - Joda DateTimeFormatter - 方法 ofPattern(String) 未定义

android - 为什么我的 AlarmManager 会立即启动?

c - 这些时钟之间有什么区别, `real-time clock` ,`wall clock` ,`system clock` ?

PHP 商店营业时间晚上

firebase - FCM HTTP v1 : how to get access token using Go?

file - 在golang中发送图像而不下载它

mongodb - 使用golang从MongoDB批量获取记录