当前位置:网站首页>go time. after
go time. after
2022-06-30 14:09:00 【Zhen Jie.】
Golang time after characteristic use
time after Trigger , Example :
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("Start Time =", time.Now())
a := time.After(5*time.Second)
fmt.Println(<-a)
fmt.Println("End Start Time =", time.Now())
}
After program running , The output is shown in the following figure :
We see ,time After Waiting for 5 Seconds later , Returned a read-only channel. therefore , If you will After() In the select One of the statement blocks case in , Then you can make others case There is a certain length of time to listen and read 、 Write events .
If there are others within this period of time case Not readable yet 、 Can write event , This After() Where case Will end the current select, And then terminate select ( If select Not in loop ) Or enter the next round select ( If select In circulation ).
time after Set timeout , Example
package main
import (
"fmt"
"time"
)
func main() {
ch1 := make(chan string)
// Activate a goroutine, but 5 Seconds before sending data
go func() {
time.Sleep(5 * time.Second)
ch1 <- "put value into ch1"
}()
select {
case val := <-ch1:
fmt.Println("recv value from ch1:",val)
return
// Just wait 3 second , And then it's over
case <-time.After(3 * time.Second):
fmt.Println("3 second over, timeover")
}
}
After program running , The output is shown in the following figure :
There is a timeout phenomenon on it , because Newly activated goroutine Wait first 5 second , Then send the data to channel ch1 in . however main goroutine Continue to run to select Sentence block , Because of the first case Conditions not met ( Be careful ,main goroutine It won't block ).
Evaluate the second case when , Will perform time.After() wait for 3 second ,3 Seconds later, the channel data returned by this function is read , So it's time to case Satisfy select Conditions , The select Because it's not in the loop , So just end ,main goroutine And so it ends . From beginning to end , Newly activated goroutine No chance to send data to ch1 in .
边栏推荐
- MySQL如何将列合并?
- Unity animator parameter
- [KALI] KALI系统、软件更新(附带镜像源)
- 科普达人丨漫画图解什么是eRDMA?
- Multi terminal collaboration of Huawei accounts to create a better internet life
- Golang template (text/template)
- MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
- What network security problems are exposed when a large-scale QQ number theft event occurs?
- Deep understanding Net (2) kernel mode 2 Kernel mode construct semaphone
- Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source!
猜你喜欢

There is no utf8 option for creating tables in Navicat database.
![[kali] Kali system, software update (with image source)](/img/ac/43a3f81d50ab6866271b500b142252.png)
[kali] Kali system, software update (with image source)

remote: Support for password authentication was removed on August 13, 2021. Please use a personal ac

Loss function: Diou loss handwriting implementation

Waving flags and shouting for basic language

【科学文献计量】外文文献及中文文献关键词的挖掘与可视化

IM即时通讯应用开发中无法解决的“顽疾”

Google Earth Engine(GEE)——GHSL:全球人类住区层,建成网格 1975-1990-2000-2015 (P2016) 数据集
![[scientific research data processing] [basic] category variable frequency analysis chart, numerical variable distribution chart and normality test (including lognormal)](/img/70/8bf226964118efb324ca4d339df654.png)
[scientific research data processing] [basic] category variable frequency analysis chart, numerical variable distribution chart and normality test (including lognormal)

嵌入式开发:5个可能不再被禁止的C特征
随机推荐
Race of golang
Pit used by go language array type
Lifting scanning tool
Impersonate server and client using message queuing
Publicity of the fourth batch of shortlisted Enterprises - annual Top100 smart Internet supplier selection
想請教一下,我在佛山,到哪裏開戶比較好?手機開戶是安全麼?
Complete TCP forwarding server (kernel linked list + mutex)
Knowledge dissemination cannot replace professional learning!
数据库表为什么写不进数据了
[scientific research data processing] [practice] frequency analysis chart of category variables, distribution chart of numerical variables and normality test (including lognormal)
[kubernetes series] k8s set mysql8 case insensitive
Deep understanding Net (2) kernel mode 2 Kernel mode construct semaphone
Simple understanding of the difference between get request and post submission
Realize a simple LAN communication (similar to feiqiu)
Deep understanding Net (2) kernel mode 3 Kernel mode construct mutex
Google Earth engine (GEE) - ghsl: global human settlements layer, built grid 1975-1990-2000-2015 (p2016) data set
How to take the first step in digital transformation
点击table的td单元格出现dialog弹窗,获取值后将值放回td单元格
Apache Doris Compaction優化百科全書
Numpy creates an empty array data = np empty(shape=[1, 64,64,3])