在很多golang net/http
文章中,一个请求返回两个值:response和error:
resp, err := http.Get("http://example.com/")
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
对于http相关的错误,会在resp
中返回,状态码如502、400等。可能返回的错误有哪些?在我处理它们之前,我需要先了解它们。
最佳答案
- 网址解析错误
- 重定向次数过多
- tcp 连接\写入\读取超时
- 302 状态,但位置 header 为空
等等
你可以阅读 http 包中的源代码。然后你可以找到这个函数http.Get
返回的所有错误。
关于http - 什么是可能的 golang http 响应错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40857132/