go - 从net/http包中的* urlError访问Timeout()方法

标签 go

我使用Client.Get(url)函数访问网页。

timeout := time.Duration(wgetTimeout) * time.Second
client := http.Client{Timeout: timeout}

// Get page and check for error (timeout, http ...)
res, err := client.Get(url)
if err != nil {
    return "", err
}
defer res.Body.Close()
Client.Get() function doc说:

返回的任何错误均为* url.Error类型。如果请求超时,则url.Error值的Timeout方法将报告true。

如何访问类似bool Timeout()的url.Error方法?
here发布的解决方案要求使用Client.Do()函数。

最佳答案

GoDoc: Type *url.Error

if e, ok := err.(*url.Error); ok && e.Timeout() {
    log.Fatal("timeout is: ", e.Timeout())
} else if err != nil {
    panic(err)
}

关于go - 从net/http包中的* urlError访问Timeout()方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62496603/

相关文章:

go - 如何从 Golang 中的 http 响应中读取压缩数据

go - proxy背后如何使用Golang的go mod依赖管理?

go - HyperLedger Fabric 获取区 block 信息 - Fabric Go SDK

go - 在 Go 中处理需要访问数据库的中间件

postgresql - 需要使用 golang 在 postgreSQL 中将日期增加 1 年

file - 是否可以在 Golang 中调用代码的地方获取文件名?

mongodb - MongoDB管道中的多个顶级聚合查询

Go OS 文件从文档中读取示例不起作用 - 声明

go - 输入 ssh 提示数据

go - 在 golang 中,如何从函数内重新分配外部引用?