golang中的定时器是使用的chanel阻塞来实现的,主要使用到了time包中的内容,如果有多个定时器的channel,为了防止阻塞,可以使用select来获取遍历channel

定时器获取的channel是个单通道channel,只能读不能写,定义时这样来定义var test <-chan int

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.NewTicker(time.Second)
    t1 := time.NewTicker(time.Second * 2)
    for {
        select {
        case v := <-t.C:
            fmt.Println("hello", v)
        case v := <-t1.C:
            fmt.Println("tsh", v)
        }
    }

    // var test <-chan int
    // <-test
}

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄