In golang what does `something` mean

huangapple go评论70阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2015年11月5日 06:22:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/33533219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定