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

2022-07-26,,,,

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 实现每隔几分钟执行一个函数.doc》

下载本文的Word格式文档,以方便收藏与打印。