douzhen5158 2018-12-26 02:08
浏览 199

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

I'm currently learning Golang (and so far I love it). But unfortunately, I've been stuck for a couple hours and I don't seem to find any solution to my problem on Google.

So here's my problem. I have this piece of code (from a tutorial) :

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)

    }

}

I know, my code is incomplete but that's because I deleted the parts that were not causing the problem to make it more readable on Stackoverflow.

So when I get the content of Location and try to process the data with ioutil.ReadAll() I get this error mentioning a pointer :

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

I really don't understand this error, no matter how much I look into it. I tried to pull the error from ioutil.ReadAll(resp.Body) by doing _, e := ioutil.ReadAll(resp.Body) and then printing e, but doing that throws another error...

I read somewhere that it can be because the body returned to me has errors, but it's working fine in the tutorial.

Hopefully you guys will have a solution for me. Thanks.

EDIT : Here are the structs I have defined :

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条回答 默认 最新

  • dongsimang4036 2018-12-26 03:15
    关注

    As Mostafa mentioned , you have to handle the errors properly. There is no try catch in golang. Something like this i have tired. atleast it catpures url error is in 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()
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误