golang sleep
版權宣告:本文為翔雲原創文章,未經博主允許不得轉載。https://blog.csdn.net/lanyang123456/article/details/78158457
golang的休眠可以使用time包中的sleep。
函式原型為:
func Sleep(d Duration)
其中的Duration定義為:
type Duration int64
Duration的單位為 nanosecond。
為了便於使用,time中定義了時間常量:
const (Nanosecond Duration = 1Microsecond = 1000 * NanosecondMillisecond = 1000 * MicrosecondSecond = 1000 * MillisecondMinute = 60 * SecondHour = 60 * Minute)
Example
下面實現休眠2秒功能。
package main import ( "fmt" "time" ) func main() { fmt.Println("begin") time.Sleep(time.Duration(2)*time.Second) fmt.Println("end") }
程式碼實現休眠2秒功能。