问题
I'm using net.http.client
After sending the request with
resp, err := Client.Do(req)
I'm getting the error I want to work with.
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)
Sorry for dumb question, I'm totally new to golang.
回答1:
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() {
// ..
}
来源:https://stackoverflow.com/questions/60150236/how-to-unwrap-url-error-returned-from-http-client