[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.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
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM