go 整分钟开始执行程序

前言

有时候我们的程序要求整分钟开始运行,这时候就需要对当前时间进行判断

package main

import (
	"fmt"
	"time"
)

func main() {
	for {
		nowTime := time.Now().Unix()
		//判断是否是整分钟,执行一次我们要运行的程序,跳出循环
		if nowTime%60 == 0 {
			go operation()
			break
		}
		fmt.Println(nowTime % 60)
		time.Sleep(time.Second)
	}

	select {}
}

func operation() {
	for {
		fmt.Println("operation...")
		time.Sleep(time.Second)
	}
}
posted @ 2021-04-27 19:23  牛奔  阅读(107)  评论(0编辑  收藏  举报