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 =(