Go 错误合集

handler/net/net.go:21: using resp before checking for errors

func HttpGet(url string) (response string) {
    client := http.Client{Timeout: 5 * time.Second}
    resp, err := client.Get(url)
    //这一行就是21行
    defer resp.Body.Close()
    if err != nil {
        panic(err)
    }
    ......
}

所以 我们把 defer resp.Body.Close()这一行移动到 err 判断下面即可

func HttpGet(url string) (response string) {
    client := http.Client{Timeout: 5 * time.Second}
    resp, err := client.Get(url)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    ......
}

request canceled while waiting for connection

Get xxxxx: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

这个可能是因为本地DNS问题

参考文章