Golang——time.Ticker定时器

ticker一般用来作为时间周期执行命令:

func main() {ticker := time.NewTicker(time.Second) // 每隔1s进行一次打印for {<-ticker.Cfmt.Println("这是ticker的打印")}
}

Ticker结构体

// A Ticker holds a channel that delivers ``ticks'' of a clock
// at intervals.
type Ticker struct {C <-chan Time // The channel on which the ticks are delivered.r runtimeTimer
}

Ticker保管一个通道,并每隔一段时间向其传递"tick"。

创建时钟周期

time.NewTicker()

func NewTicker(d Duration) *Ticker
NewTicker返回一个新的Ticker,该Ticker包含一个通道字段,并会每隔时间段d就向该通道发送当时的时间。它会调整时间间隔或者丢弃tick信息以适应反应慢的接收者。如果d<=0会panic。关闭该Ticker可以释放相关资源

实例1:在11点35分打印"Golang"

func main() {myTicker:=time.NewTicker(time.Second)      //设置时间周期for{nowTime:=<-myTicker.C       //当前时间if nowTime.Hour()==11 && nowTime.Minute()==35{fmt.Println("Golang")break}}
}

实例2:每秒打印时间,然后10秒后结束。

package mainimport ("fmt""time"
)func main() {ticker := time.NewTicker(time.Second)defer ticker.Stop()done := make(chan bool)go func() {time.Sleep(10 * time.Second)done <- true}()for {select {case <-done:fmt.Println("Done!")returncase t := <-ticker.C:fmt.Println("Current time: ", t)}}
}

输出结果

Current time:  2022-10-21 09:52:47.7603062 +0800 CST m=+1.029829101
Current time:  2022-10-21 09:52:48.7591099 +0800 CST m=+2.028632801
Current time:  2022-10-21 09:52:49.7620993 +0800 CST m=+3.031622201
Current time:  2022-10-21 09:52:50.7534455 +0800 CST m=+4.022968401
Current time:  2022-10-21 09:52:51.7596767 +0800 CST m=+5.029199601
Current time:  2022-10-21 09:52:52.7634561 +0800 CST m=+6.032979001
Current time:  2022-10-21 09:52:53.7499603 +0800 CST m=+7.019483201
Current time:  2022-10-21 09:52:54.7570725 +0800 CST m=+8.026595401
Current time:  2022-10-21 09:52:55.7489688 +0800 CST m=+9.018491701
Done!

使用Tick()方法

// Tick is a convenience wrapper for NewTicker providing access to the ticking
// channel only. While Tick is useful for clients that have no need to shut down
// the Ticker, be aware that without a way to shut it down the underlying
// Ticker cannot be recovered by the garbage collector; it "leaks".
// Unlike NewTicker, Tick will return nil if d <= 0.
func Tick(d Duration) <-chan Time {if d <= 0 {return nil}return NewTicker(d).C
}

实例

// 60秒定期更新一次tick := time.Tick(60 * time.Second)go func() {for {<-tickerr  = do_something(...)if err != nil {panic(err)}}}()

参考文章

传送门1

传送门2

Golang——time.Ticker定时器相关推荐

  1. Golang当中的定时器

    定时器 前言 定时器的基本使用 前言 在平时写代码的时候,我们经常会遇到在将来某个时间点或者间隔一段时间重复执行函数.这个时候我们就可以考虑使用定时器.本片文章主要介绍一下golang当中的几个常用的 ...

  2. golang延时_Golang 定时器底层实现深度剖析

    本文将基于 Golang 源码对 Timer 的底层实现进行深度剖析.主要包含以下内容: 1. Timer 和 Ticker 在 Golang 中的底层实现细节,包括数据结构等选型. 2. 分析 ti ...

  3. golang中的定时器

    定时器 type Timer struct {C<-chan Timer runtimeTimer } Timer 一个定时器,代表未来的一个单一事件,你可以告诉timer你要等待多长时间,它提 ...

  4. Golang停止ticker断续器

    1.原代码如下 package mainimport ("fmt""time" )func main() {intchan := make(chan int,1 ...

  5. Golang的Ticker使用姿势

    golang 中的ticker 直接就是代码 package mainimport ("fmt""time" )var Ticker *time.Tickerf ...

  6. Golang Study 一 定时器使用

    通用的定时器使用例子,包括创建定时器和重置定时器. package mainimport ("fmt""time" //time包 )func main() { ...

  7. ticker定时器用法

    package mainimport ("fmt""time" )func foo() {fmt.Println("wilson...") ...

  8. golang延时_Go 定时器/延时触发器

    Go 可以借助 time.After/time.Ticker 来实现延迟/定时触发器,主要原理是借助无缓冲channel无数据时读取操作会阻塞当前协程,Go会在给定的时间后向channel中写入一些数 ...

  9. Let‘s Go Rust 系列之定时器 Ticker Timer

    前言 在实际项目开发中,经常会有定时任务的功能开发需求,定时任务主要分为两种, 1,在固定的时刻执行某个任务,也就是 Timer 2,基于固定的时间间隔,周期的执行某个任务,也就是Ticker ​ 很 ...

最新文章

  1. angular 缓存 html5,Angular 如何及时更新客户端缓存?
  2. nc/netcat/ncat/nmap/socat Tips
  3. Flash 组件应用与开发
  4. springmvc学习(一)
  5. Linux 修改密码
  6. [Redis6]新数据类型_HyperLogLog
  7. 同一网络下的两台计算机怎样共享,同一路由器上的两台计算机如何共享文件?...
  8. 网页制作之各种框架简介
  9. 安科瑞 【节能学院】电气火灾监控系统在太焦铁路博爱站房项目的应用
  10. 小马哥-----高仿苹果6s plus刷机拆机主板型号Q39主板图与开机界面图 分版本
  11. 第三方支付的商户订单号和支付交易号
  12. debian linux 7 安装,Debian 7安装设置教程
  13. 红皮书再总结——实例四
  14. Origin 不连续数据点做出连续曲线
  15. 工作站就是高级的微型计算机,家用pc机和工作站有什么不同?
  16. 国外聊天工具下载网址
  17. 转载:你的同龄人正在抛弃你
  18. 百度aistudio上使用yolov4训练人脸检测模型
  19. springboot中使用freemarker生成word循环输出图片(二维码)
  20. 【黑盒测试用例设计】等价类划分法

热门文章

  1. angular.js使用路由时,子控制器监听不到父级$boardcast的事件
  2. 基于JavaEE的健身器材产品专卖店管理系统_JSP网站设计_MySQL数据库设计
  3. win7虚拟机时间不能修改怎么办
  4. u盘数据恢复,教你轻松搞定!
  5. 用计算机撩人套路,给大家一些撩人的套路情话~
  6. 塔防游戏路点---编辑器脚本设计
  7. android wifi热点setting
  8. 关于游戏打击感的帖子[转]
  9. 【《操作系统慕课版》合集】期末复习 + 核心算法整理 + 课后答案
  10. python调用百度地图实现导航_利用python和百度地图API实现数据地图标注