Go 使用http.Get()时内存地址无效或无指针取消引用
我刚开始学习Go lang,我编写了一个小演示,从txt读取图片URL,将URL放入数组,然后将响应保存到文件中 这是我的密码Go 使用http.Get()时内存地址无效或无指针取消引用,go,Go,我刚开始学习Go lang,我编写了一个小演示,从txt读取图片URL,将URL放入数组,然后将响应保存到文件中 这是我的密码 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 := bufi
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()
}
}
运行它,我得到了
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,每个url一行
有人帮忙吗?这对我来说似乎很好
func main() {
randomURLs := []string{"http://i.imgur.com/I7Rak2y.jpg", "http://i.imgur.com/XuM8GCN.jpg"}
fetchPic(randomURLs)
}
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")
defer 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")
}
}
}
前面的代码输出以下结果:
Running...
key is : 0,this line is http://i.imgur.com/I7Rak2y.jpg
load ok
Write ok
length is 445661key is : 1,this line is http://i.imgur.com/XuM8GCN.jpg
load ok
Write ok
length is 746031
Success: process exited with code 0.
这似乎对我很有效
func main() {
randomURLs := []string{"http://i.imgur.com/I7Rak2y.jpg", "http://i.imgur.com/XuM8GCN.jpg"}
fetchPic(randomURLs)
}
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")
defer 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")
}
}
}
前面的代码输出以下结果:
Running...
key is : 0,this line is http://i.imgur.com/I7Rak2y.jpg
load ok
Write ok
length is 445661key is : 1,this line is http://i.imgur.com/XuM8GCN.jpg
load ok
Write ok
length is 746031
Success: process exited with code 0.
在执行
httpRequest之后,您正在无条件地从httpRequest.Body读取,错误:=http.Get(string(value))
而不检查错误:如果http.Get
失败,您将无法从中读取有效的httpRequest.Body
经验法则:立即检查每一个错误。您正在无条件地从httpRequest.Body读取。在执行
httpRequest之后,err:=http.Get(string(value))
而不检查错误:如果http.Get
失败,您将无法从中读取有效的httpRequest.Body
经验法则:立即检查每个错误。我键入此代码并进行测试,没有发现任何问题
package main
import (
"os"
"fmt"
"bufio"
"net/http"
"io/ioutil"
)
func read_file(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
reader := bufio.NewScanner(file)
for reader.Scan() {
lines = append(lines, reader.Text())
}
return lines, reader.Err()
}
func main(){
lines, err := read_file("./file.txt")
if err != nil {
fmt.Println(err)
return
}
for key, value := range lines {
fmt.Printf("key is : %d,this line is %s \n\n", key, value)
httpRequest, err := http.Get(value)
if err != nil {
fmt.Println(err)
return
}
defer httpRequest.Body.Close()
result, readErr := ioutil.ReadAll(httpRequest.Body)
if readErr != nil {
fmt.Println(err)
return
}
ioutil.WriteFile("./" + fmt.Sprintf("%d", key) + ".jpg", result, 0777)
}
}
答复
key is : 0,this line is http://samsam.iteye.com/images/login_icon.png
key is : 1,this line is http://www.iteye.com/images/logo-small.gif
壳
我键入此代码并进行测试,没有任何问题
package main
import (
"os"
"fmt"
"bufio"
"net/http"
"io/ioutil"
)
func read_file(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
reader := bufio.NewScanner(file)
for reader.Scan() {
lines = append(lines, reader.Text())
}
return lines, reader.Err()
}
func main(){
lines, err := read_file("./file.txt")
if err != nil {
fmt.Println(err)
return
}
for key, value := range lines {
fmt.Printf("key is : %d,this line is %s \n\n", key, value)
httpRequest, err := http.Get(value)
if err != nil {
fmt.Println(err)
return
}
defer httpRequest.Body.Close()
result, readErr := ioutil.ReadAll(httpRequest.Body)
if readErr != nil {
fmt.Println(err)
return
}
ioutil.WriteFile("./" + fmt.Sprintf("%d", key) + ".jpg", result, 0777)
}
}
答复
key is : 0,this line is http://samsam.iteye.com/images/login_icon.png
key is : 1,this line is http://www.iteye.com/images/logo-small.gif
壳
fetch.go:40是“httpRequest.Body.Close()”fetch.go:28是“fetchPic(picUrl)”,其中在main()中我调用了函数,您忽略了(可能的)错误,我怀疑这些错误会对您有所帮助。fetch.go:40是“httpRequest.Body.Close()”fetch.go:28是“fetchPic(picUrl)”,我在main()中调用了函数,你忽略了(可能的)错误,我怀疑这些错误对你有帮助。谢谢你,伙计,我已经发布了我所有的代码,我的main()函数中有错误吗?似乎没问题…@rainyluo你的代码的其余部分似乎没问题,但我不知道
meinv.txt
谢谢你,伙计,我已经发布了我所有的代码,我的main()函数是否有任何错误?似乎没有问题…@rainyluo您的代码的其余部分似乎还可以,但我不知道meinv.txt