golang 实现每隔几分钟执行一个函数
时间:2021-06-20 08:48:16|栏目:Golang|点击: 次
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表数据,结算完成: %v\n",time.Now())
//以下为定时执行的操作
BoottimeSettlement()
}
}
栏 目:Golang
下一篇:go 迭代string数组操作 go for string[]
本文标题:golang 实现每隔几分钟执行一个函数
本文地址:http://www.codeinn.net/misctech/144895.html






