最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Golang time.Sleep()用法及示例講解

 更新時(shí)間:2023年02月28日 09:25:10   作者:純凈天空  
Go語(yǔ)言中的Sleep()函數(shù)用于在至少規(guī)定的持續(xù)時(shí)間d內(nèi)停止最新的go-routine,這篇文章主要介紹了Golang time.Sleep()用法及示例講解,需要的朋友可以參考下

在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”。

到此這篇關(guān)于Golang time.Sleep()用法及代碼示例的文章就介紹到這了,更多相關(guān)Golang time.Sleep()用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

大同市| 河北省| 大足县| 志丹县| 三都| 上饶市| 深水埗区| 巴彦淖尔市| 安阳市| 平远县| 肇东市| 辛集市| 星座| 大化| 佛坪县| 张家港市| 杭州市| 商洛市| 安泽县| 夹江县| 鹿邑县| 和田县| 和平区| 宿州市| 虞城县| 巴楚县| 威信县| 资源县| 揭阳市| 灵山县| 永新县| 遂昌县| 大名县| 崇左市| 隆德县| 石嘴山市| 天气| 绍兴县| 新和县| 长宁县| 绥中县|