您的位置:首页 > 编程语言 > Go语言

go语言定时器

2014-07-12 10:43 190 查看
[plain] view
plaincopy

package main

import "fmt"

import "time"

func main() {

t := time.NewTimer(2 * time.Second)

//v := <- t.C

//fmt.Println(v)

go onTime(t.C)

fmt.Println("main thread")

time.Sleep(10 * time.Second)

}

func onTime(c <-chan time.Time) {

for now := range c {

// now := <- c

fmt.Println("onTime", now)

}

}

[plain] view
plaincopy

package main

import "fmt"

import "time"

func main() {

time.AfterFunc(5 * time.Second, f1)

time.AfterFunc(2 * time.Second, f2)

fmt.Println("main thread")

time.Sleep(10 * time.Second)

}

func f1() {

fmt.Println("f1 done !")

}

func f2() {

fmt.Println("f2 done !")

}

[plain] view
plaincopy

package main

import "fmt"

import "time"

var count int = 0

func main() {

t := time.Tick(2 * time.Second)

i := 0

for now := range t {

fmt.Println(now, doSomething())

i++

if i > 10 {

break

}

}

}

func doSomething() int {

count++

return count

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: