Golang 如何使用超时
当我们不想等待某些 goroutines 的输出时,超时发挥了重要作用。需要注意的是,Go并不直接支持超时,但我们可以毫无困难地实现它们。
假设我们有一个情况,我们想从一个通道 ch 中接收一些值,但是我们不想等待超过3秒的时间来接收这个值。如果我们在规定的3秒后得到了输出,那么我们想丢弃它并打印一个不同的消息,而不是等待更长的时间来输出。
例子1
让我们先来探讨一个简单的案例,即我们在较长的时间内从一个函数中获得输出。
请看下面的代码。
package main
import (
"fmt"
"time"
)
func timeConsuming() string {
time.Sleep(5 * time.Second)
return "The timeConsuming() function has stopped"
}
func main() {
currentChannel := make(chan string, 1)
go func() {
text := timeConsuming()
currentChannel <- text
}()
select {
case res := <-currentChannel:
fmt.Println(res)
}
fmt.Println("Main function exited!")
}
在上面的代码中,我们有一个名为 timeConsuming() 的函数,它表示一个可能在较长或所需时间后返回一个特定值的函数的情况。一个例子是,在一个网络请求中,获取数据花费了太多的时间,用户感到很沮丧。
在上述代码的 主 函数中,我们有一个缓冲通道,然后我们在 select语句 的帮助下等待数据的到来 。 因此,在上述情况下,整个代码将不得不等待,直到函数timeConsuming()完成其工作。
输出
如果我们用 go run main.go 命令运行上述代码,那么我们将得到以下输出。
The timeConsuming() function has stopped
Main function exited!
例2
现在,让我们假设我们不想等待函数 timeConsuming() 完成其执行。在这种情况下,我们可以使用 时间 包的 After() 函数。
语法
After() 函数的语法是。
func After(d Duration) −- chan Time
After 函数等待 d duration结束,然后它将返回一个通道的当前时间。
请看下面的代码,我们利用 After 函数来注册一个超时。
package main
import (
"fmt"
"time"
)
func timeConsuming() string {
time.Sleep(5 * time.Second)
return "The timeConsuming() function has stopped"
}
func main() {
currentChannel := make(chan string, 1)
go func() {
text := timeConsuming()
currentChannel <- text
}()
select {
case res := <-currentChannel:
fmt.Println(res)
case <-time.After(3 * time.Second):
fmt.Println("Out of time :(")
}
fmt.Println("Main function exited!")
}
输出
如果我们用 go run main.go 命令运行上述代码,那么我们将得到以下输出。
Out of time :(
Main function exited!