I learning the go and struggling with this panic error... it working for some time, but soon end up with such exception panic: runtime error: invalid memory address or nil pointer dereference
The function simply iterate over map of proxies until it successfully get the content of the "address". It must be not very idiomatic, especially using map instead of slice and the last return, but I hope that is not the reason of panic crush... if I omitted something possible important please, let me know, I will update the post, I just do not want to flood it with unnecessary info. proxies is a struct with map field with methods for concurrent safe 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