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
}
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
}
相关文章推荐
- Go语言实现定时器的方法
- Go语言中定时器cron的基本使用教程
- Go语言错误处理小例子
- Go语言编程-初级
- Go语言中cannot convert adminname (type interface {}) to type *: need type assertion的解决办法
- Go语言获取数组长度的方法
- Go语言写基数排序
- Go语言循环判断的使用~
- 调试go语言出现:exec: "gcc": executable file not found in %PATH%
- Go语言扫描目录并获取相关信息的方法
- gops - Go语言程序查看和诊断工具
- Go语言学习笔记二: 变量
- go语言执行windows下命令行的方法
- go语言的延迟队列客户端
- go语言中的slice
- go 语言初体验
- go 语言学习历程
- Go语言Web框架gwk介绍4
- GO语言基础及特性
- Go语言程序的命令行参数