-1
我遇到了問題。 我需要編寫一個函數來填充從元素名稱-p,div,span等等到HTML文檔樹中具有該名稱的元素的映射。我做了功能outline2
,它不工作,這裏是eroor日誌:Golang中的counetrs html標籤的地圖不能正常工作
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)
}
}
請指出我的錯誤。我不知道該怎麼辦=(
哦,閣下=(。這樣一個愚蠢的錯誤。非常感謝,它有幫助。 –