我的功能开始。然后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/