में जाओ documentation for xml:Unmarshal वहाँ एक उदाहरण है कि इस एक्सएमएलसीधे बनाम परोक्ष रूप से नेस्ट structs
<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>
unmarshals इन 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
}
ध्यान दें कि Result
अलग Address
परिभाषित के लिए एक संदर्भ होता है। जाहिर है, यह कोड काम करता है।
जब मैं इस एक्सएमएल
<C>
<D>
<E>Fred</E>
<F>42</F>
</D>
</C>
unmarshal करने के लिए इन structs
type D struct {
E string
F int
}
type C struct { // Compiles OK but result empty.
D
}
उपयोग करने का प्रयास मैं खाली परिणाम {{ 0}}
मिलता है। हालांकि struct नीचे उत्पादन ठीक काम करता है {{Fred 42}}
type C struct { // This works.
D struct {
E string
F int
}
}
Playground example देखें।
क्या मुझे structs के बारे में कुछ सूक्ष्म बिंदु याद आ रही है?