time实现定时器

package main

import (
    "fmt"
    "time"
)

func task()  {
    fmt.Println(time.Now().Local())
}

func main() {
    task()
    t := time.NewTimer(time.Second * 5)
    for {
        select{
        case <-t.C:
            t.Reset(time.Second * 5)
            task()
        }
    }

}

 

 

package main

import (
    "fmt"
    "time"
)

func task()  {
    fmt.Println(time.Now().Local())
}

func main() {
    t := time.NewTicker(3 * time.Second)
    for {
        select {
        case <-t.C:
            task()
        }
    }

}

 

posted @ 2020-03-24 11:12  眼镜儿  阅读(342)  评论(0编辑  收藏  举报