英文:
In golang what does `something` mean
问题
我有这个XML读取器结构体:
type Recurlyservers struct {
XMLName xml.Name `xml:"servers"`
Version string `xml:"version,attr"`
Svs []server `xml:"server"`
Description string `xml:",innerxml"`
}
xml:"servers"
和xml:"version,attr"
的含义是什么?我不知道这个``是什么。我想在Google上搜索,但我不知道它的名称。它是什么?我能否在没有这个的情况下使用标准结构体?因为没有这个,XML读取无法正常工作。
英文:
I have this XML reader struct:
type Recurlyservers struct {
XMLName xml.Name `xml:"servers"`
Version string `xml: "version,attr"`
Svs []server `xml: "server"`
Description string `xml:",innerxml"`
}
What is the meaning of this xml:"servers"
or xml: "version,attr"
? I don't know what this `` is. I would like to search in Google but I don't know it's name. What is it? and can I use the standard struct without this? Because the XML reading is not working without this.
答案1
得分: 5
这些被称为字段标签。它们被XML编码器/解码器用于将属性名称映射到实际数据中的值。在你的例子中,它们是完全必要的,因为XML中的字段是小写的,而在Go语言中,要导出结构体的字段,它们必须是大写的。由于XML名称与你的类型上的字段名称不同,你必须指定编码包中的字段对应关系。
几乎所有的数据转换/编码/存储库都使用相同的约定。
英文:
Those are called field tags. They're used by the xml encoder/decoder to map the property names to the values in the actual data. In your example they're completely necessary because the fields in XML are lower cased, in Go to have fields on a struct exported they have to be upper cased. Since the xml names diverge from the field names on your type, you have to specify what goes where for the encoding package.
This same convention is used from almost all data transformation/encoding/storage libraries.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论