golang 实现每隔几分钟执行一个函数

浏览:49日期:2022-08-29

1、使用定时器

2、使用这种方式

go function() func function() { // TODO 具体逻辑 // 每5分钟执行一次 time.AfterFunc(5*time.Minute, function)}

补充:Golang:每天零点定时执行操作

我就废话不多说了,大家还是直接看代码吧~

import ( 'time' 'fmt')//定时结算Boottime表数据func BoottimeTimingSettlement() { for { now := time.Now() // 计算下一个零点 next := now.Add(time.Hour * 24) next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location()) t := time.NewTimer(next.Sub(now)) <-t.C Printf('定时结算Boottime表数据,结算完成: %vn',time.Now()) //以下为定时执行的操作 BoottimeSettlement() }}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持乐呵呵网。如有错误或未考虑完全的地方,望不吝赐教。

标签: Golang
相关文章: