Http 使用ioutil.ReadAll()的Golang中的“无效内存地址”

Http 使用ioutil.ReadAll()的Golang中的“无效内存地址”,http,pointers,go,Http,Pointers,Go,我目前正在学习Golang,到目前为止我很喜欢它。但不幸的是,我被困了几个小时,在谷歌上似乎找不到任何解决问题的方法 这就是我的问题。我有一段教程中的代码: func main() { var s SitemapIndex resp, _ := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml") bytes, _ := ioutil.ReadAll(resp.Body) resp.

我目前正在学习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)

    }

}
我知道,我的代码是不完整的,但那是因为我删除了没有引起问题的部分,以使它在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
我真的不明白这个错误,不管我怎么研究它。我试图从ioutil.ReadAllresp.Body中提取错误,方法是:执行u,e:=ioutil.ReadAllresp.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
}

正如Mostafa提到的,您必须正确处理错误。golang没有试捕。 像这样的东西我已经累了。 liteIde中至少存在it catpures url错误

package main

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

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("Unable to get the url ")
        os.Exit(1)
    }
    bytes, _ := ioutil.ReadAll(resp.Body)
    defer resp.Body.Close()
    //fmt.Println(string(bytes))
    xml.Unmarshal(bytes, &s)
    //fmt.Println(len(s.Locations))

    for _, Location := range s.Locations {
        //fmt.Println(Location)
        go func() {
            r, err := http.Get(Location)
            if err != nil {
                fmt.Println("Error occured ", err)
                return
            }
            bytes, err := ioutil.ReadAll(resp.Body)
            if err != nil {
                fmt.Printf("Error in reading :", err)
            }
            fmt.Println(string(bytes))
            r.Body.Close()
        }()
    }
    runtime.Gosched()
}
围棋的第一条规则:检查错误

当函数调用返回错误时,这是调用方的错误 检查并采取适当措施的责任

通常,当函数返回非nil错误时,其其他结果为 未定义,应忽略

比如说,

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
panic: runtime error: invalid memory address or nil pointer dereference
输出:

"\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
panic: runtime error: invalid memory address or nil pointer dereference

好了,我找到了问题的原因和解决方法。问题是url有一些我没有看到的新行

因此,与其这样做,不如:

resp, err := http.Get(Location)
我这样做:

resp, err := http.Get(strings.TrimSpace(Location))

这就解决了问题。

你能复制SitemapIndex进行检查吗?谢谢,我刚刚编辑了postYou忽略的错误,这样没有人能找到问题。不要丢弃错误,而是处理它们。这样地。resp,er:=http.Getyour地址;如果出错!=nil{log.Fatalnerr}Cross发布:谢谢你的回答。所以我按照别人告诉我的那样打印了错误,它说URL中的第一个路径段不能包含冒号。顺便说一句,你知道为什么吗?我用goroutine编写了测试代码,但你可能不得不在没有goroutine的情况下尝试。调试打印中的每个字符,查看是否有任何附加字符与位置url一起添加。错误表明url有问题,需要进行检查。我们如何编写缓冲区或搅拌,需要确保它没有url以外的任何其他字符我自己尝试过,最后添加了runtime.Gosched,但没有解决问题。顺便说一句,这行的作用是什么?好的,我的建议是,查看范围循环中的URL位置URL,并检查它是否添加了任何附加引号或其他内容。Goshed调用确保主程序在所有goroutine完成之前不会退出