定时器

Golang

package main

import (
	"fmt"
	"time"
)

var stop = make(chan bool)

func main() {
	toRun()
}

// 简单定时器
func toRun() {
	go func() {
		nt := time.NewTimer(1 * time.Second)
		fmt.Println("=========== starting")
		defer func() {
			nt.Stop()
			stop <- true
		}()
		for {
			select {
			case <-stop:
				{
					goto EXIT
				}
			case <-nt.C:
				{
					fmt.Println("=========== running")
					fmt.Println(time.Now().Format("2006/01/02/15:04:05"))
					nt.Reset(time.Second * 5)
					// 退出条件 分钟数大于等于10 程序终止
					if time.Now().Minute() >= 10 {
						fmt.Println(time.Now().Minute())
						stop <- true
					}
				}
			}
		}
	EXIT:
	}()
	for {
		select {}
	}

}

 

你可能感兴趣的:(个人随笔,go,golang)