zoukankan      html  css  js  c++  java
  • go实现定时功能两种方法

    1:timer

    学习自:https://studygolang.com/articles/2479

    timer1 := time.NewTimer(time.Second * 2)
    
    //此处在等待channel中的信号,执行此段代码时会阻塞两秒
    
    <-timer1.C
    
    timer1 := time.NewTimer(time.Second * 2)
    
    //此处在等待channel中的信号,执行此段代码时会阻塞两秒
    
    <-timer1.C
    
    
    ticker := time.NewTimer(time.Second)
    for_ =range ticker.C{
    
    }

    或者

    for {
        select {
        case <-timer.C:
        func()
        }
    }

    timer中:

    type Ticker struct {
    	C <-chan Time // The channel on which the ticks are delivered.
    	r runtimeTimer
    }
    C只能读不能写
    https://studygolang.com/articles/4565

    2:"github.com/robfig/cron"中的cron

    package main

    import (
    "github.com/robfig/cron"
    "log"
    )

    func main() {
    i := 0
    c := cron.New()
    spec := "*/5 * * * * ?"
    c.AddFunc(spec, func() {
    i++
    log.Println("cron running:", i)
    })
    c.AddFunc("@every 1h1m", func() {
    i++
    log.Println("cron running:", i)
    })
    c.Start()
    }

  • 相关阅读:
    EXCEL自动导出HTML
    亡灵序曲超清
    支持国产动画-唐伯卿和曾小兰
    中国表情
    logging 日志
    datetime库运用
    hashlib 加密
    os2
    python json数据处理
    python操作redis
  • 原文地址:https://www.cnblogs.com/newcoder/p/7906859.html
Copyright © 2011-2022 走看看