網(wǎng)站首頁 編程語言 正文
在Go語言中,時間包提供了確定和查看時間的函數(shù)。 Go語言中的Sleep()函數(shù)用于在至少規(guī)定的持續(xù)時間d內(nèi)停止最新的go-routine。睡眠時間為負(fù)數(shù)或零將導(dǎo)致此方法立即返回。此外,此函數(shù)在時間包下定義。在這里,您需要導(dǎo)入“time”包才能使用這些函數(shù)。
用法:
func Sleep(d Duration)
此處,d是睡眠時間,以秒為單位。
返回值:它將在規(guī)定的時間內(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.....
在這里,在運行上述代碼后,當(dāng)調(diào)用main函數(shù)時,由于使用了Sleep方法,因此在給定的時間內(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”中,由于超時的持續(xù)時間(在After()方法中)大于睡眠時間(在Sleep()方法中)而被打印,因此,在顯示超時之前打印輸出,但是在此之后,以下情況發(fā)生了超時持續(xù)時間小于睡眠時間,因此在打印輸出之前顯示超時,因此將打印“timeout….2”。
原文鏈接:https://vimsky.com/examples/usage/time-sleep-function-in-golang-with-examples.html
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-10-17 .Net?Core使用Coravel實現(xiàn)任務(wù)調(diào)度的完整步驟_實用技巧
- 2022-09-22 Python 短路運算符
- 2022-01-10 egg作為后端接口,在前端調(diào)用
- 2022-04-11 關(guān)于指令重排現(xiàn)象的兩個階段詳解_相關(guān)技巧
- 2022-10-23 Python繪制牛奶凍曲線(高木曲線)案例_python
- 2022-07-19 Python數(shù)據(jù)分析之NumPy常用函數(shù)使用詳解_python
- 2022-06-13 go語言編程之select信道處理示例詳解_Golang
- 2023-02-05 Python中import語句用法案例講解_python
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支