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

时间:2018-12-26 02:08:15

标签: http pointers go

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

无论我怎么看,我真的都不明白这个错误。我尝试通过执行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
}

3 个答案:

答案 0 :(得分:2)

第一个执行规则:检查错误。


  

函数调用返回错误时,它是调用者的   负责检查并采取适当措施。

     

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

     

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
        }
    }
}

答案 1 :(得分:1)

如Mostafa所述,您必须正确处理错误。在golang中没有任何尝试。 这样的事情我很累。 至少Catpures网址错误位于liteIde

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()
}

答案 2 :(得分:0)

好的,所以我已经找到了造成问题的原因以及解决方法。问题在于网址中有一些我看不到的换行符。

所以不要这样做:

resp, err := http.Get(Location)

我这样做了:

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

解决了。