在Golang中,计数器的HTML标签映射不正常。

huangapple go评论83阅读模式
英文:

Map of counetrs html tags in Golang dont work properly

问题

我遇到了一个问题。
我需要编写一个函数,将元素名称(如p、div、span等)映射到HTML文档树中具有该名称的元素数量。我编写了一个名为outline2的函数,但它不起作用,这是错误日志:

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

以下是代码:

func main() {
	doc, err := html.Parse(os.Stdin)
	if err != nil {
		fmt.Fprintf(os.Stderr, "outline: %v\n", 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)
	}
}

请指出我的错误。我不知道该怎么办 =(

英文:

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\n", 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

得分: 1

错误是准确的。你不能在空映射中进行赋值。你必须先分配空间。另外,你的函数没有返回任何内容,所以应该这样做:

outline := make(map[string]int) //分配并命名你的映射
outline2(outline, doc)
fmt.Println(outline) //对其进行操作

应该可以工作。

英文:

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

huangapple
  • 本文由 发表于 2017年1月28日 01:15:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/41899501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定