golang中的定时器是使用的chanel阻塞来实现的,主要使用到了time包中的内容,如果有多个定时器的channel,为了防止阻塞,可以使用select来获取遍历channel
定时器获取的channel是个单通道channel,只能读不能写,定义时这样来定义var test <-chan int
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
}