Go XML Marshal: Unintended Output of Slice

The marshaling and unmarshaling rules are well laid out in the relevant documentation of encoding/xml. For example the section on xml.Marshal says:

If a field uses a tag “a>b>c”, then the element c will be nested inside parent elements a and b. Fields that appear next to each other that name the same parent will be enclosed in one XML element.


So you should be able to achieve what you want with this:

type Package struct {
    Name  string
    Files []File `xml:"Files>File"`
}

// this also works
type Package struct {
    Name  string
    Files []File `xml:">File"`
}

https://play.golang.org/p/gg-6Tj3WNnV

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top