Golang定时器

2017-09-28  本文已影响0人  Feng_Sir

http://www.jb51.net/article/61277.htm
本文实例讲述了Go语言实现定时器的方法。分享给大家供大家参考。具体实现方法如下:

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()
}
上一篇下一篇

猜你喜欢

热点阅读