【发布时间】:2017-02-27 15:30:05
【问题描述】:
在 Go documentation for xml:Unmarshal 中有一个解组此 xml 的示例
<Person>
<FullName>Grace R. Emlin</FullName>
<Company>Example Inc.</Company>
<Email where="home">
<Addr>gre@example.com</Addr>
</Email>
<Email where='work'>
<Addr>gre@work.com</Addr>
</Email>
<Group>
<Value>Friends</Value>
<Value>Squash</Value>
</Group>
<City>Hanga Roa</City>
<State>Easter Island</State>
</Person>
使用这些结构
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 时
<C>
<D>
<E>Fred</E>
<F>42</F>
</D>
</C>
使用这些结构
type D struct {
E string
F int
}
type C struct { // Compiles OK but result empty.
D
}
我得到空结果{{ 0}}。但是下面的结构可以正常工作产生{{Fred 42}}
type C struct { // This works.
D struct {
E string
F int
}
}
我是否遗漏了一些关于结构的微妙之处?
【问题讨论】: