golang sleep

180it 2021-09-23 PM 1359℃ 0条

golang的休眠可以使用time包中的sleep。
函数原型为:

func Sleep(d Duration)

其中的Duration定义为:

type Duration int64

Duration的单位为 nanosecond。

为了便于使用,time中定义了时间常量:

const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 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秒功能。
————————————————

原文链接:https://blog.csdn.net/lanyang123456/article/details/78158457

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

golang sleep