有什么可能golang http响应错误?

ciz*_*ixs 1 http go

与许多golang net/http文章一样,请求返回两个值:响应和错误:

resp, err := http.Get("http://example.com/")
if err != nil {
    // handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
Run Code Online (Sandbox Code Playgroud)

对于与http相关的错误,它将返回resp状态代码,如502,400等.返回的可能错误是什么?我需要知道它们才能处理它们.

Lar*_*y.Z 7

  1. URL解析错误
  2. 重定向时间过多
  3. tcp connect\write\read timeout
  4. 302状态,但为null位置标头

等等

您可以在http包中阅读源代码.然后您可以找到此函数返回的所有错误http.Get.