網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
在Go語(yǔ)言中,時(shí)間包提供了確定和查看時(shí)間的函數(shù)。 Go語(yǔ)言中的Sleep()函數(shù)用于在至少規(guī)定的持續(xù)時(shí)間d內(nèi)停止最新的go-routine。睡眠時(shí)間為負(fù)數(shù)或零將導(dǎo)致此方法立即返回。此外,此函數(shù)在時(shí)間包下定義。在這里,您需要導(dǎo)入“time”包才能使用這些函數(shù)。
用法:
func Sleep(d Duration)
此處,d是睡眠時(shí)間,以秒為單位。
返回值:它將在規(guī)定的時(shí)間內(nèi)暫停最新的go-routine,然后在睡眠結(jié)束后返回任何操作的輸出。
范例1:
// Golang program to illustrate the usage of // Sleep() function ?? // Including main package package main ?? // Importing fmt and time import ( ????"fmt" ????"time" ) ?? // Main function func main() { ?? ????// Calling Sleep method ????time.Sleep(8 * time.Second) ?? ????// Printed after sleep is over ????fmt.Println("Sleep Over.....") }
輸出:
Sleep Over.....
在這里,在運(yùn)行上述代碼后,當(dāng)調(diào)用main函數(shù)時(shí),由于使用了Sleep方法,因此在給定的時(shí)間內(nèi)停止了所述操作,然后打印了結(jié)果。
范例2:
// Golang program to illustrate the usage of // Sleep() function ?? // Including main package package main ?? // Importing time and fmt import ( ????"fmt" ????"time" ) ?? // Main function func main() { ?? ????// Creating channel using ????// make keyword ????mychan1:= make(chan string, 2) ?? ????// Calling Sleep function of go ????go func() { ????????time.Sleep(2 * time.Second) ?? ????????// Displayed after sleep overs ????????mychan1 <- "output1" ????}() ?? ????// Select statement ????select { ?? ????// Case statement ????case out:= <-mychan1: ????????fmt.Println(out) ?? ????// Calling After method ????case <-time.After(3 * time.Second): ????????fmt.Println("timeout....1") ????} ?? ????// Again Creating channel using ????// make keyword ????mychan2:= make(chan string, 2) ?? ????// Calling Sleep method of go ????go func() { ????????time.Sleep(6 * time.Second) ?? ????????// Printed after sleep overs ????????mychan2 <- "output2" ????}() ?? ????// Select statement ????select { ?? ????// Case statement ????case out:= <-mychan2: ????????fmt.Println(out) ?? ????// Calling After method ????case <-time.After(3 * time.Second): ????????fmt.Println("timeout....2") ????} }
輸出:
output1
timeout....2
此處,在上面的代碼“output1”中,由于超時(shí)的持續(xù)時(shí)間(在After()方法中)大于睡眠時(shí)間(在Sleep()方法中)而被打印,因此,在顯示超時(shí)之前打印輸出,但是在此之后,以下情況發(fā)生了超時(shí)持續(xù)時(shí)間小于睡眠時(shí)間,因此在打印輸出之前顯示超時(shí),因此將打印“timeout….2”。
原文鏈接:https://vimsky.com/examples/usage/time-sleep-function-in-golang-with-examples.html
- 上一篇:沒(méi)有了
- 下一篇:沒(méi)有了
相關(guān)推薦
- 2023-02-09 利用C++開(kāi)發(fā)一個(gè)protobuf動(dòng)態(tài)解析工具_(dá)C 語(yǔ)言
- 2023-07-25 SpringBoot整合Quartz以及異步調(diào)用
- 2022-11-07 python多進(jìn)程使用apply_async的使用方法詳解_python
- 2022-04-24 C++的靜態(tài)成員變量和靜態(tài)成員函數(shù)你了解多少_C 語(yǔ)言
- 2022-05-21 Ingress七層路由機(jī)制實(shí)現(xiàn)域名的方式訪問(wèn)k8s_服務(wù)器其它
- 2022-04-27 .Net?Core中使用MongoDB搭建集群與項(xiàng)目實(shí)戰(zhàn)_基礎(chǔ)應(yīng)用
- 2023-10-26 在el-table中根據(jù)判斷不同值顯示對(duì)應(yīng)文本
- 2022-03-21 Android中Binder?IPC機(jī)制介紹_Android
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支