2017-01-27 4 views
-1

Ich habe ein Problem. Ich muss eine Funktion schreiben, um ein Mapping von Elementnamen - p, div, span usw. - auf die Anzahl der Elemente mit diesem Namen in einer HTML-Dokumentstruktur aufzufüllen. Ich habe Funktion outline2, es funktioniert nicht, hier ist die eroor log:Karte von couetrs HTML-Tags in Golang funktionieren nicht richtig

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 

Hier ist der 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) 


    } 
} 

Bitte, meine Fehler hinweisen. Ich weiß nicht, was zu tun = (

Antwort

1

Fehler genau ist. Sie können nicht in nil Karte zuordnen. Sie müssen zuerst zuordnen. Auch arbeiten Sie etwas brauchen, Func Sie nichts zurück. So

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

sollte funktionieren

+0

Oh, Herr = (. So ein dummer Fehler. Vielen Dank, es hat geholfen. –

Verwandte Themen