go语言定时操作
来源:互联网 发布:网络推广方式 编辑:程序博客网 时间:2024/06/05 00:39
ticker
Timers are for when you want to do something once in the future - tickers are for when you want to do something repeatedly at regular intervals.
这个对象以指定的时间间隔重复的向通道 C 发送时间值
ticker := time.NewTicker(time.Millisecond * 500)
go func() {
for t := range ticker.C { //当管道关闭,管道内数据全部读出后,遍历会停止
fmt.Println("Tick at", t)
}
/*
for {
t := <-ticker.C
fmt.Println("Tick at", t)
}
*/
}()
time.Sleep(time.Millisecond * 1500)
ticker.Stop()
fmt.Println("Ticker stopped")
Tickers can be stopped just like timers using the Stop()
method, as shown at the bottom of the example.
timer
timer := time.NewTimer(time.Second)
go func() {
<- timer.C
println("Timer expired")
}()
stop := timer.Stop()
println("Timer cancelled:", stop)
In this case we canceled the timer before it had a chance to expire (Stop()
would return false if we tried to cancel it after it expired.)
Sleep
fmt.Println("start sleeping")
time.Sleep(time.Second)
fmt.Println("sleeping end")
After
tc:=time.After(time.Second) //返回一个time.C这个管道,1秒(time.Second)后会在此管道中放入一个时间点 //(time.Now())时间点记录的是放入管道那一刻的时间值
<-tc //阻塞中,直到取出tc管道里的数据
AfterFunc
多少时间之后在goroutine line执行函数
f := func() {
fmt.Println("Time out")
}
time.AfterFunc(1*time.Second, f)
time.Sleep(2 * time.Second) //要保证主线比子线“死的晚”,否则主线死了,子线也等于死了
- go语言定时操作
- go 语言文件操作
- Go语言 操作二进制文件
- Go语言操作json
- Go语言操作Redis
- go语言文件操作
- Go语言Slice操作.
- Go语言操作Redis
- GO语言操作MySQL
- go语言 mysql操作
- go 语言 mongodb操作
- Go语言操作json
- Go语言文件操作
- GO语言IO操作
- Go语言 Go操作mysql简单示例
- 【GO语言】文件操作例子
- GO语言的mysql操作
- Go语言atomic原子操作
- Android 自动更新
- CentOS 6.x源码安装Netdata
- Java StringBuilder和StringBuffer用法
- vim产生的临时.swp文件
- find find_if search
- go语言定时操作
- Java数据类型简介
- spring之所以不用new对象!
- 文章标题
- 自定义控件之绘图篇(四) —— Canvas变换与操作
- optimizing magento for peak performance Magento的峰值性能优化
- Java原始数据类型
- oracle里用管理员权限把A用户的表授权给B用户去使用
- 【数据结构与算法】选择排序