使用http.Get()时无效的内存地址或无指针取消引用 [英] invalid memory address or nil pointer dereference when use http.Get()

查看:340
本文介绍了使用http.Get()时无效的内存地址或无指针取消引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习Go lang,我写了一个小demo,从txt读取图片url,将urls放入数组,然后将Save Response保存到文件中。



这是我的代码

 包主

导入(
bufio
fmt
io
io / ioutil
净/ http
os


func main(){
fileName:=meinv.txt
file,_:= os.Open(fileName)
$ b $ picUrl:= make([] string, 2000)
reader:= bufio.NewReader(文件)
为{
line,_,err:= reader.ReadLine()
if err!= io.EOF {
fmt.Printf(file load%s \\\
,line)
picUrl = append(picUrl,string(line))
} else {
file.Close()
break

$ b fmt.Printf(file loaded,read to download \\\

fetchPic(picUrl)

}
func fetchPic(picUrl [] string){

var file string
for key,value:= range picUrl {

fmt.Printf (关键是:%d,这一行是%s \\\
\\\
,key,value)
httpRequest,err:= http.Get(string(value))
fmt.Print(load ok \\\


httpRequest.Body.Close()
result,readErr:= ioutil.ReadAll(httpRequest.Body)
if readErr == nil {
file =pics /+ string (key)+.jpg
ioutil.WriteFile(file,result,0777)
fmt.Print(Write ok \\\

}
len:= len(字符串(结果))
fmt.Printf(长度为%d,len)
如果err == nil {
httpRequest = nil
// result = nil
} else {
fmt.Print(load falt !!!!!!!!! \\\

}
推迟httpRequest.Body.Close()
}

}

运行它,我得到了

 键是:0,这行是

加载ok
panic:运行时错误:无效内存地址或零指针解引用
[信号0xb代码= 0x1地址= 0x40 pc = 0x40123f]

goroutine 1 [running]:
runtime.panic(0x5f8300,​​0x877688)
/usr/local/go/src/pkg/runtime/panic.c:266 + 0xb6
main.fetchPic(0xc21239d000,0x38be7,0x4223c)
/home/lyn/www/goOnDev/fetch.go:40+ 0x24f
main.main()
/ home / lyn / www / goOnDev / fetch.go:28 + 0x1b8
退出状态2

meinv.txt,one每个网址的行
任何帮助?THKS

解决方案您正在阅读无条件的httpRequest.Body后做 httpRequest,err:= http.Get(string(value))不检查err:如果 http.Get 失败,您将不会拥有一个有效的 httpRequest.Body 阅读。



经验法则:立即检查每个错误。


I just start to learn Go lang,I wrote a small demo,read picture urls from txt,put urls in an array,then Save the Response into a file.

Here is my code

package main

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)

func main() {
fileName := "meinv.txt"
file, _ := os.Open(fileName)

picUrl := make([]string, 2000)
reader := bufio.NewReader(file)
for {
    line, _, err := reader.ReadLine()
    if err != io.EOF {
        fmt.Printf("file load %s \n", line)
        picUrl = append(picUrl, string(line))
    } else {
        file.Close()
        break
    }
}
fmt.Printf("file loaded,read to download \n")
fetchPic(picUrl)

}
func fetchPic(picUrl []string) {

var file string
for key, value := range picUrl {

    fmt.Printf("key is : %d,this line is %s \n\n", key, value)
    httpRequest, err := http.Get(string(value))
    fmt.Print("load ok \n")

    httpRequest.Body.Close()
    result, readErr := ioutil.ReadAll(httpRequest.Body)
    if readErr == nil {
        file = "pics/" + string(key) + ".jpg"
        ioutil.WriteFile(file, result, 0777)
        fmt.Print("Write ok \n")
    }
    len := len(string(result))
    fmt.Printf("length is %d", len)
    if err == nil {
        httpRequest = nil
        //result = nil
    } else {
        fmt.Print("load falt!!!!!!!!! \n")
    }
    defer httpRequest.Body.Close()
}

}

run it ,and I got

key is : 0,this line is  

load ok 
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x40 pc=0x40123f]

goroutine 1 [running]:
runtime.panic(0x5f8300, 0x877688)
/usr/local/go/src/pkg/runtime/panic.c:266 +0xb6
main.fetchPic(0xc21239d000, 0x38be7, 0x4223c)
/home/lyn/www/goOnDev/fetch.go:40 +0x24f
main.main()
/home/lyn/www/goOnDev/fetch.go:28 +0x1b8
exit status 2

meinv.txt ,one line per url Anyone help?THKS

解决方案

You are reading unconditional from httpRequest.Body after doing httpRequest, err := http.Get(string(value)) without checking err: If http.Get fails you won't have a valid httpRequest.Body to read from.

Rule of thumb: Check each and every error immediately.

这篇关于使用http.Get()时无效的内存地址或无指针取消引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆