In the Go documentation for xml:Unmarshal there is an example that unmarshals this xml
<Person>
<FullName>Grace R. Emlin</FullName>
<Company>Example Inc.</Company>
<Email where="home">
<Addr>[email protected]</Addr>
</Email>
<Email where='work'>
<Addr>[email protected]</Addr>
</Email>
<Group>
<Value>Friends</Value>
<Value>Squash</Value>
</Group>
<City>Hanga Roa</City>
<State>Easter Island</State>
</Person>
using these structs
type Address struct {
City, State string
}
type Result struct {
XMLName xml.Name `xml:"Person"`
Name string `xml:"FullName"`
Phone string
Email []Email
Groups []string `xml:"Group>Value"`
Address
}
Note that Result contains a reference to the separately defined Address. Obviously, this code works.
When I try to unmarshal this xml
<C>
<D>
<E>Fred</E>
<F>42</F>
</D>
</C>
using these structs
type D struct {
E string
F int
}
type C struct { // Compiles OK but result empty.
D
}
I get empty results {{ 0}}. However the struct below works OK producing {{Fred 42}}
type C struct { // This works.
D struct {
E string
F int
}
}
See Playground example.
Am I missing some subtle point about structs?