我需要输出一个 XML 文件,并且我构建了一些表示它的结构作为一个基本示例,如下所示:
type Parent struct {
XMLName xml.Name `xml:"parent"`
Name string `xml:"name,omitempty"`
Age int64 `xml:"age,omitempty"`
Child Child `xml:"child,omitempty`
}
type Child struct {
XMLName xml.Name `xml:"child,omitempty"`
Name string `xml:"name,omitempty"`
Gender string `xml:"gender,omitempty"`
Thoughts string `xml:",innerxml,omitempty"`
}
我希望当我创建一个Parent不定义子项,然后将其编组到一个 XML 文件中时......
parent := Parent{
Name: "Beatrice",
Age: "23",
}
_ = xml.MarshalIndent(parent, "", " ")
...我应该得到一个不包含child标签的 XML 文件:
<parent>
<name>Beatrice</name>
<age>23</age>
</parent>
相反,我得到这个:
<parent>
<name>Beatrice</name>
<age>23</age>
<child></child>
</parent>
为什么空<child></child>标签在那里,我怎样才能摆脱它?
繁星淼淼
相关分类