golang sleep
时间: 2023-10-01 09:03:53 浏览: 225
在Go语言中,可以使用time包中的Sleep函数来实现休眠功能。Sleep函数的原型为:
```
func Sleep(d Duration)
```
其中d表示休眠的时间,单位是纳秒。可以用time.Duration将时间转换为Duration类型。
下面是一个示例代码,实现了休眠2秒的功能:
```go
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("begin")
time.Sleep(time.Duration(2) * time.Second)
fmt.Println("end")
}
```
以上代码会在输出"begin"后休眠2秒钟,然后再输出"end"。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [golang sleep(睡眠)](https://blog.csdn.net/weixin_44014995/article/details/113836091)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [golang sleep](https://blog.csdn.net/lanyang123456/article/details/78158457)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [undefined](undefined)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文