[Go] golang定时器的使用
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 }
更多精彩