I have an XML structure that essentially, includes an array of nodes that should deserialize into a slice of a simple go struct but it's not working. Here's the code I'm working with (the comments show what I expect):
package main
import "fmt"
import "encoding/xml"
func main() {
c := Conversation{}
xml.Unmarshal(raw, &c)
fmt.Println(len(c.Dialog)) // expecting 2, not 0
fmt.Println(c.Dialog[0].Text) // expecting "Hi", not a panic
}
var raw = []byte(`<conversation>
<message>
<text>Hi</text>
</message>
<message>
<text>Bye</text>
</message>
</conversation>`)
type Conversation struct {
Dialog []Message `xml:"conversation"`
}
type Message struct {
XMLName xml.Name `xml:"message"`
Text string `xml:"text"`
}
Why isn't this working?
Playground: http://play.golang.org/p/a_d-nhcfoP