如何解开从http.Client返回的url.Error? [英] How to unwrap url.Error returned from http.Client?

查看:44
本文介绍了如何解开从http.Client返回的url.Error?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 net.http.client

使用

resp, err := Client.Do(req)

我遇到了我要处理的错误.

I'm getting the error I want to work with.

err.Error()以字符串形式返回错误.但是我需要将错误作为对象.我发现方法 Unwrap()似乎正在返回 url.Error 对象,但是我得到了 err.Unwrap undefined(类型错误没有字段)或方法展开)

err.Error() returns an error as string. But I need to work with error as an object. I found the method Unwrap() that seems to be returning an url.Error object, but I'm getting err.Unwrap undefined (type error has no field or method Unwrap)

对不起,我是golang的新手.

Sorry for dumb question, I'm totally new to golang.

推荐答案

根据文档,从 Client.Do 返回的任何错误均为 * url.Error ,但是由于方法签名显示(* Response,error),因此您必须在使用前显式转换它:

According to the documentation, any error returned from Client.Do will be *url.Error, but since the method signature says (*Response, error), you will have to convert it explicitly before use:

urlErr := err.(*url.Error)
if urlErr.Timeout() {
    // ..
}

这篇关于如何解开从http.Client返回的url.Error?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆