duanhan1392 2017-01-27 17:15
浏览 49
已采纳

Golang柜台HTML标记的地图无法正常工作

I got a problem. I need to write a function to populate a mapping from element names—p, div, span, and so on — to the number of elements with that name in an HTML document tree. I made function outline2, it's not working, here is the eroor log:

html
panic: assignment to entry in nil map

    goroutine 1 [running]:
    panic(0x4c3b40, 0xc042010a90)
            F:/Go/src/runtime/panic.go:500 +0x1af
    main.outline2(0x0, 0xc0420320e0)
            F:/Go_Stuff/Books/Golang_stuff/exercises/src/gopl.io/ch5/outline/main.go:29 +0x1ae
    main.outline2(0x0, 0xc042032070)
            F:/Go_Stuff/Books/Golang_stuff/exercises/src/gopl.io/ch5/outline/main.go:34 +0xe3
    main.main()
            F:/Go_Stuff/Books/Golang_stuff/exercises/src/gopl.io/ch5/outline/main.go:23 +0x77

Here is the code:

func main() {
    doc, err := html.Parse(os.Stdin)
    if err != nil {
        fmt.Fprintf(os.Stderr, "outline: %v
", err)
        os.Exit(1)
    }
    outline2(nil, doc)
}
func outline2(tags map[string]int, n *html.Node) {
    fmt.Println(n.Data)
    if n.Type == html.ElementNode {
        fmt.Println(n.Data)
        tags[n.Data] += 1 // push tag
        fmt.Println(n.Data)

    }
    for c := n.FirstChild; c != nil; c = c.NextSibling {
        outline2(tags, c)


    }
}

Please, point out my mistakes. I don't know what to do =(

  • 写回答

1条回答 默认 最新

  • dua55014 2017-01-27 17:33
    关注

    Error is exact. You can't assign in nil map. You must first allocate. Also you need something to work, you func return nothing. So

    outline := make(map[string]int) //allocate and name your map
    outline2(outline, doc)
    fmt.Println(outline) //do something with it
    

    should work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端