在轉到documentation for xml:Unmarshal有,使用這些結構直接VS間接嵌套結構
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
參考解編此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>
一個例子。很明顯,這段代碼有效。
當我嘗試使用這些結構
type D struct {
E string
F int
}
type C struct { // Compiles OK but result empty.
D
}
解組此XML
<C>
<D>
<E>Fred</E>
<F>42</F>
</D>
</C>
我得到空的結果{{ 0}}
。但是下面的結構工程確定生產{{Fred 42}}
type C struct { // This works.
D struct {
E string
F int
}
}
我是否錯過了關於結構的一些微妙點?