Go语言的定时器示例代码

2021-11-01 10:41:05 浏览数 (1)

代码语言:javascript复制
package main
import (
    "fmt"
    "time"
)
func testTimer1() {
    go func() {
        fmt.Println("test timer1")
    }()
}
func testTimer2() {
    go func() {
        fmt.Println("test timer2")
    }()
}
func timer1() {
    timer1 := time.NewTicker(1 * time.Second)
    for {
        select {
        case <-timer1.C:
            testTimer1()
        }
    }
}
func timer2() {
    timer2 := time.NewTicker(2 * time.Second)
    for {
        select {
        case <-timer2.C:
            testTimer2()
        }
    }
}
func main() {
    go timer1()
    timer2()
}</pre> 

0 人点赞