网络编程
位置:首页>> 网络编程>> Go语言>> golang 实现每隔几分钟执行一个函数

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

作者:chenchongswjtu  发布时间:2024-05-22 17:48:14 

标签: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()
 }
}

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

来源:https://blog.csdn.net/chenchongg/article/details/86741047

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com