当前位置:网站首页>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 .
边栏推荐
- Talk about Vue's two terminal diff algorithm, analysis of the rendering principle of the mobile terminal, and whether the database primary key must be self incremented? What scenarios do not suggest s
- "As a service", the inevitable choice of enterprise digital transformation
- Chapter 13 signal (III) - example demonstration
- How does MySQL merge columns?
- Google Earth engine (GEE) -- converts string to number and applies it to time search (ee.date.fromymd)
- [redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
- Simple understanding of the difference between get request and post submission
- Flat shading with unity
- Observable, seulement fiable: première bombe de salon de la série cloudops d'exploitation et d'entretien automatisés dans le nuage
- Waving flags and shouting for basic language
猜你喜欢

About the problems encountered when using the timer class to stop with a button (why does the QPushButton (for the first time) need to be clicked twice to respond?)

navicat数据库建表是没有utf8选项。

QQ was stolen? The reason is

Intelligent operation and maintenance: visual management system based on BIM Technology

mysql拒绝访问、管理员身份打开的

Defi "where does the money come from"? A problem that most people don't understand

Unity Animator 参数

香港回归20余年,图扑数字孪生港珠澳大桥,超震撼

Rpm2rpm packaging steps

Jetpack compose for perfect screen fit
随机推荐
Mysql database foundation: stored procedures and functions
"As a service", the inevitable choice of enterprise digital transformation
数字化转型道阻且长,如何迈好关键的第一步
I'd like to ask you, where can I open an account in Foshan? Is it safe to open a mobile account?
Jetpack Compose 实现完美屏幕适配
【观察】智能产业加速,为何AI算力要先行?
I want to ask how to open an account at China Merchants Securities? Is it safe to open a stock account through the link
[scientific research data processing] [practice] frequency analysis chart of category variables, distribution chart of numerical variables and normality test (including lognormal)
How to take the first step in digital transformation
Je suis à Foshan, où puis - je ouvrir un compte? L'ouverture d'un compte par téléphone mobile est - elle sécurisée?
get请求与post提交区别的简易理解
Introduction to reverse commissioning - VA and RVA conversion in PE 04/07
With the development of industrial Internet, the landing and application of the Internet has become wider
步骤详解 | 助您轻松提交 Google Play 数据安全表单
想請教一下,我在佛山,到哪裏開戶比較好?手機開戶是安全麼?
Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source!
This editor will open source soon!
Deep understanding Net (2) kernel mode 2 Kernel mode construct semaphone
MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
Unity 频繁切换分支 结果模型出现莫名其妙的错误