gpt4 book ai didi

http - Golang 中的 "Invalid memory address"使用 ioutil.ReadAll()

转载 作者:数据小太阳 更新时间:2023-10-29 03:31:26 27 4
gpt4 key购买 nike

我目前正在学习 Golang(到目前为止我很喜欢它)。但不幸的是,我已经被困了几个小时,而且我似乎没有在 Google 上找到任何解决我的问题的方法。

所以这是我的问题。我有这段代码(来自教程):

func main() {
var s SitemapIndex

resp, _ := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
bytes, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()

xml.Unmarshal(bytes, &s)

for _, Location := range s.Locations {
resp, _ := http.Get(Location)
ioutil.ReadAll(resp.Body)

}

}

我知道,我的代码不完整,但那是因为我删除了不会导致问题的部分,以使其在 Stackoverflow 上更具可读性。

因此,当我获取 Location 的内容并尝试使用 ioutil.ReadAll() 处理数据时,我收到此错误提示:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x1210a69]

goroutine 1 [running]:
main.main()
/Users/tom/Developer/Go/src/news/index.go:23 +0x159
exit status 2

无论我如何调查,我真的不明白这个错误。我试图通过执行 _, e := ioutil.ReadAll(resp.Body) 然后打印 ioutil.ReadAll(resp.Body) 中提取错误e,但这样做会引发另一个错误...

我在某处读到这可能是因为返回给我的正文有错误,但它在教程中运行良好。

希望你们能为我提供解决方案。谢谢。

编辑:这是我定义的结构:

type SitemapIndex struct {
Locations []string `xml:"sitemap>loc"`
}

type News struct {
Titles []string `xml:"url>news>title"`
Keywords []string `xml:"url>news>keywords"`
Locations []string `xml:"url>loc"`
}

type NewsMap struct {
Keyword string
Location string
}

最佳答案

Go 的第一条规则:检查错误。


When a function call returns an error, it’s the caller’s responsibility to check it and take appropriate action.

Usually when a function returns a non-nil error, its other results are undefined and should be ignored.

The Go Programming Language, Alan A. A. Donovan and Brian W. Kernighan


例如,

if err != nil {
fmt.Printf("%q\n", Location) // debug error
fmt.Println(resp) // debug error
fmt.Println(err)
return
}

输出:

"\nhttps://www.washingtonpost.com/news-sitemaps/politics.xml\n"
<nil>
parse
https://www.washingtonpost.com/news-sitemaps/politics.xml
: first path segment in URL cannot contain colon

如果你没有捕获到这个错误并继续 resp == nil 那么

bytes, err := ioutil.ReadAll(resp.Body)

输出:

panic: runtime error: invalid memory address or nil pointer dereference

package main

import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"strings"
)

type SitemapIndex struct {
Locations []string `xml:"sitemap>loc"`
}

func main() {
var s SitemapIndex

resp, err := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
if err != nil {
fmt.Println(err)
return
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
err = resp.Body.Close()
if err != nil {
fmt.Println(err)
return
}

err = xml.Unmarshal(bytes, &s)
if err != nil {
fmt.Println(err)
return
}

for _, Location := range s.Locations {
resp, err := http.Get(Location)
if err != nil {
fmt.Printf("%q\n", Location) // debug error
fmt.Println(resp) // debug error
fmt.Println(err)
return
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(len(bytes))
err = resp.Body.Close()
if err != nil {
fmt.Println(err)
return
}
}
}

关于http - Golang 中的 "Invalid memory address"使用 ioutil.ReadAll(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53926818/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com