当前位置:网站首页>Golang standard library time
Golang standard library time
2022-06-22 03:23:00 【ManNiaoQinFen】
Timer
Timer seeing the name of a thing one thinks of its function , It means timer , Some timing operations can be realized , Inside is also through channel To achieve .
package main
import (
"fmt"
"time"
)
func main() {
t1 := time.Now()
fmt.Printf("t1: %v\n", t1)
<-time.After(time.Second * 2) // wait for 2 Second method 1
t2 := time.Now()
fmt.Printf("t2: %v\n", t2)
timer2 := time.NewTimer(time.Second * 2) // wait for 2 Second method 2
<-timer2.C
t3 := time.Now()
fmt.Printf("t3: %v\n", t3)
time.Sleep(time.Second * 2) // wait for 2 Second method 3
t4 := time.Now()
fmt.Printf("t4: %v\n", t4)
timer3 := time.NewTimer(time.Second)
go func() {
<-timer3.C
fmt.Println("Timer 3 expired")
}()
stop := timer3.Stop() // Stop timer
// prevent timer events , When the function executes ,timer The timer stops , The corresponding event is no longer executed
if stop {
fmt.Println("Timer 3 stop")
}
timer4 := time.NewTimer(time.Second * 5) // Original setting 5s
timer4.Reset(time.Second * 4) // Reset time , Restart the clock
<-timer4.C
t5 := time.Now()
fmt.Printf("t5: %v\n", t5)
}
Ticker
Timer Only once ,Ticker Can be executed periodically .
example 1
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(time.Second)
counter := 1
for _ = range ticker.C {
fmt.Println("ticker 1")
counter++
if counter >= 3 {
ticker.Stop()
break
}
}
}
Running results
ticker 1
ticker 1
example 2
package main
import (
"fmt"
"time"
)
func main() {
chanInt := make(chan int)
ticker := time.NewTicker(time.Second)
go func() {
for _ = range ticker.C {
select {
case chanInt <- 1:
case chanInt <- 2:
case chanInt <- 3:
}
}
}()
sum := 0
for v := range chanInt {
fmt.Printf(" receive : %v\n", v)
sum += v
if sum >= 10 {
fmt.Printf("sum: %v\n", sum)
break
}
}
}
Running results
Receiving : 2
receive : 1
receive : 1
receive : 2
receive : 1
receive : 2
receive : 2
sum: 11
边栏推荐
- php使用composer
- Adaptive batch job scheduler: automatically derive parallelism for Flink batch jobs
- Unity3d C # generates non repeated random numbers in the interval
- Usage of tail
- TX2 attaching SD card, jtop
- uv_ loop_ Init() process
- 【 thesis 】 zero reference depth curve estimation for low light image enhancement
- [nvme2.0b 5] sous - système nvm
- [pwn basics]pwntools learning
- NXP imx8mp learning records
猜你喜欢

uv_run流程

【NVMe2.0b 6】NVMe 队列模型

Overview of web framework and program development

Library management system (PHP final report)

高考后网上查询信息,注意防范没有 SSL证书的网站
![[nvme2.0b 8] nvme queue arbitration mechanism](/img/35/c5098623c14749711b205ef97c34a7.png)
[nvme2.0b 8] nvme queue arbitration mechanism

简单入门到彻底搞懂防抖和节流

2022 low voltage electrician test question simulation test question bank and answers

A solution to memory leak in server

table标签的不规则布局
随机推荐
Splunk: Auto load Balanced TCP Output issue
微信小程序onPageScroll无效
AtCoder Beginner Contest 252(dijkstra,逆向思维)
Harmonyos Hongmeng uses ORM bee to access database instances
[nvme2.0b 12] NVM capacity model
Project management software development project management
【Kubernetes 系列】Kubernetes 是什么?
克隆的vmware虚拟主机网卡无法启动解决办法
golang并发编程之Mutex互斥锁实现同步和原子操作(atomic)
Hosting environment, components, APIs, collaboration and publishing of applets
360edr planing
On map state mapping
策略模式
六、MySQL之数据定义语言(一)
圖數據庫ONgDB Release v-1.0.2
【NVMe2.0b 12】NVM 容量模型
【NVMe2.0b 5】NVM Subsystem
Force buckle 239 Sliding window Max
[nvme2.0b 9] controller initialization process
不规范的命名