[Go] golang定时器的使用

前端之家收集整理的这篇文章主要介绍了[Go] golang定时器的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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.Second * 2)
    for {
        selectcase v := <-t.C:
            fmt.Println(hello,v)
        t1.C:
            tsh// var test <-chan int
     <-test
}

 

猜你在找的Go相关文章