登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-29 08:09:32 443浏览 收藏

大家好,今天本人给大家带来文章《Golang 中使用 ioutil.ReadAll() 出现“无效内存地址”》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

问题内容

我目前正在学习 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) 然后打印 eioutil.readall(resp.body) 中提取错误,但这样做会引发另一个错误...

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

希望你们能给我一个解决方案。谢谢。

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

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
}

解决方案


好的,我已经找到问题的原因以及解决方法了。问题是该网址有一些我没有看到的换行符。

所以不要这样做:

resp, err := http.get(location)

我这样做了:

resp, err := http.get(strings.trimspace(location))

解决了这个问题。

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

例如,

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

本篇关于《Golang 中使用 ioutil.ReadAll() 出现“无效内存地址”》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>