我目前正在学习 Golang(到目前为止我很喜欢它)。但不幸的是,我已经被困了几个小时,而且我似乎在谷歌上找不到任何解决我的问题的方法。
所以这是我的问题。我有这段代码(来自教程):
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)
}
}
Run Code Online (Sandbox Code Playgroud)
我知道,我的代码不完整,但那是因为我删除了不会导致问题的部分,以使其在 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
Run Code Online (Sandbox Code Playgroud)
无论我如何研究它,我真的不明白这个错误。ioutil.ReadAll(resp.Body)我试图通过执行_, e := 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
}
Run Code Online (Sandbox Code Playgroud)
Go 的第一条规则:检查错误。
\n\n\n\n\n当函数调用返回错误时,调用者有责任检查错误并采取适当的操作。
\n\n通常,当函数返回非零错误时,其其他结果是未定义的,应被忽略。
\n\n \n
例如,
\n\nif err != nil {\n fmt.Printf("%q\\n", Location) // debug error\n fmt.Println(resp) // debug error\n fmt.Println(err)\n return\n}\nRun Code Online (Sandbox Code Playgroud)\n\n输出:
\n\n"\\nhttps://www.washingtonpost.com/news-sitemaps/politics.xml\\n"\n<nil>\nparse \nhttps://www.washingtonpost.com/news-sitemaps/politics.xml\n: first path segment in URL cannot contain colon\nRun Code Online (Sandbox Code Playgroud)\n\n如果您没有发现此错误并继续resp == nil执行
bytes, err := ioutil.ReadAll(resp.Body)\nRun Code Online (Sandbox Code Playgroud)\n\n输出:
\n\npanic: runtime error: invalid memory address or nil pointer dereference\nRun Code Online (Sandbox Code Playgroud)\n\npackage main\n\nimport (\n "encoding/xml"\n "fmt"\n "io/ioutil"\n "net/http"\n "strings"\n)\n\ntype SitemapIndex struct {\n Locations []string `xml:"sitemap>loc"`\n}\n\nfunc main() {\n var s SitemapIndex\n\n resp, err := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")\n if err != nil {\n fmt.Println(err)\n return\n }\n bytes, err := ioutil.ReadAll(resp.Body)\n if err != nil {\n fmt.Println(err)\n return\n }\n err = resp.Body.Close()\n if err != nil {\n fmt.Println(err)\n return\n }\n\n err = xml.Unmarshal(bytes, &s)\n if err != nil {\n fmt.Println(err)\n return\n }\n\n for _, Location := range s.Locations {\n resp, err := http.Get(Location)\n if err != nil {\n fmt.Printf("%q\\n", Location) // debug error\n fmt.Println(resp) // debug error\n fmt.Println(err)\n return\n }\n bytes, err := ioutil.ReadAll(resp.Body)\n if err != nil {\n fmt.Println(err)\n return\n }\n fmt.Println(len(bytes))\n err = resp.Body.Close()\n if err != nil {\n fmt.Println(err)\n return\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n