图源 "invalid memory address ... " 与 http.client
Figure source of "invalid memory address ... " with http.client
我正在学习并与这个恐慌错误作斗争......它工作了一段时间,但很快就出现了这样的异常panic: runtime error: invalid memory address or nil pointer dereference
该函数简单地遍历代理映射,直到成功获取 "address" 的内容。它一定不是很地道,尤其是使用 map 而不是 slice 和最后一个 return,但我希望这不是 panic crush 的原因......
如果我遗漏了一些可能重要的东西,请告诉我,我会更新 post,我只是不想用不必要的信息淹没它。
proxies 是一个带有 map 字段的结构,带有并发安全 read/delete 的方法。
func getContent(address string) string {
localProxies := proxies.Get()
for proxy := range localProxies {
proxyUrl, _ := url.Parse("http://" + proxy)
transport := http.Transport{
Dial: dialTimeout,
Proxy: http.ProxyURL(proxyUrl),
}
httpClient := http.Client{Transport: &transport, Timeout: timeout}
req, err := http.NewRequest("GET", address, nil)
if err != nil {
fmt.Println("Request error: ", err.Error())
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0")
res, err := httpClient.Do(req)
defer res.Body.Close()
if err != nil {
// fmt.Println("Broken ", proxy)
fmt.Println("Response error: ", err.Error())
proxies.Del(proxy)
continue
}
if res.StatusCode != 200 {
// fmt.Println("Status error: ", res.Status)
proxies.Del(proxy)
continue
}
out, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println("Read error: ", err.Error())
proxies.Del(proxy)
continue
}
return string(out)
}
return "error"
}
go version go1.4.2 linux/amd64
在我看到更多信息之前,这只是基于我从您发布的代码中收集到的信息的猜测。
来电
res.Body.Close()
应该在检查错误之后出现,而不是之前。所以改变:
res, err := httpClient.Do(req)
defer res.Body.Close()
if err != nil {
...
}
到
res, err := httpClient.Do(req)
if err != nil {
...
}
defer res.Body.Close()
我正在学习并与这个恐慌错误作斗争......它工作了一段时间,但很快就出现了这样的异常panic: runtime error: invalid memory address or nil pointer dereference
该函数简单地遍历代理映射,直到成功获取 "address" 的内容。它一定不是很地道,尤其是使用 map 而不是 slice 和最后一个 return,但我希望这不是 panic crush 的原因...... 如果我遗漏了一些可能重要的东西,请告诉我,我会更新 post,我只是不想用不必要的信息淹没它。 proxies 是一个带有 map 字段的结构,带有并发安全 read/delete 的方法。
func getContent(address string) string {
localProxies := proxies.Get()
for proxy := range localProxies {
proxyUrl, _ := url.Parse("http://" + proxy)
transport := http.Transport{
Dial: dialTimeout,
Proxy: http.ProxyURL(proxyUrl),
}
httpClient := http.Client{Transport: &transport, Timeout: timeout}
req, err := http.NewRequest("GET", address, nil)
if err != nil {
fmt.Println("Request error: ", err.Error())
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0")
res, err := httpClient.Do(req)
defer res.Body.Close()
if err != nil {
// fmt.Println("Broken ", proxy)
fmt.Println("Response error: ", err.Error())
proxies.Del(proxy)
continue
}
if res.StatusCode != 200 {
// fmt.Println("Status error: ", res.Status)
proxies.Del(proxy)
continue
}
out, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println("Read error: ", err.Error())
proxies.Del(proxy)
continue
}
return string(out)
}
return "error"
}
go version go1.4.2 linux/amd64
在我看到更多信息之前,这只是基于我从您发布的代码中收集到的信息的猜测。
来电
res.Body.Close()
应该在检查错误之后出现,而不是之前。所以改变:
res, err := httpClient.Do(req)
defer res.Body.Close()
if err != nil {
...
}
到
res, err := httpClient.Do(req)
if err != nil {
...
}
defer res.Body.Close()