golang中的定時器是使用的chanel阻塞來實現的,主要使用到了time包中的內容,若是有多個定時器的channel,爲了防止阻塞,可使用select來獲取遍歷channelgolang
定時器獲取的channel是個單通道channel,只能讀不能寫,定義時這樣來定義var test <-chan intspa
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 }