What is the use of tag in golang struct?

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

What is the use of tag in golang struct?

问题

我不理解结构标签的意义。我一直在研究它们,并注意到它们可以与反射包(reflect package)一起使用。但我不知道它们的实际用途。

结构标签(struct tags)是用于给结构体的字段添加元数据的。它们以字符串的形式写在字段的后面,用于描述字段的含义、用途或其他相关信息。结构标签通常在编写代码时不会直接使用,而是在运行时通过反射来获取和使用。

结构标签在很多框架和库中被广泛使用,用于实现各种功能,例如数据验证、ORM(对象关系映射)、JSON/XML序列化和反序列化等。通过在结构体字段上添加标签,我们可以在运行时动态地获取和解析这些标签,从而实现一些自动化的操作。

在你提供的代码示例中,结构体TagType的字段都有相应的结构标签。这些标签描述了字段的重要性、名称和数量。在实际应用中,可以通过反射来获取这些标签,并根据需要进行处理。

总之,结构标签是一种元数据机制,可以为结构体字段添加描述信息,通过反射可以在运行时获取和使用这些标签,从而实现一些自动化的功能。

英文:

I don't understand the significance of struct tags. I have been looking them, and noticed that they can be used with reflect package. But I don't know any practical uses of them.

type TagType struct { // tags
    field1 bool   “An important answer”
    field2 string “The name of the thing”
    field3 int    “How much there are”
}

答案1

得分: 8

标签的使用强烈依赖于结构体的使用方式。

一个典型的用法是为持久化或序列化添加规范或约束。

例如,在使用JSON解析器/编码器时,标签用于指定如何从JSON中读取结构体或将其写入JSON,当不使用默认的编码方案(即字段的名称)时。

以下是来自json包文档的几个示例:

// 该字段被此包忽略。
Field int json:"-"

// 该字段在JSON中以键"myName"出现。
Field int json:"myName"

// 该字段在JSON中以键"myName"出现,并且如果其值为空,则从对象中省略该字段,如上所定义。
Field int json:"myName,omitempty"

// 该字段在JSON中以键"Field"(默认)出现,但如果为空,则跳过该字段。
// 注意前导逗号。
Field int json:",omitempty"

英文:

The use of tags strongly depends on how your struct is used.

A typical use is to add specifications or constraints for persistence or serialisation.

For example, when using the JSON parser/encoder, tags are used to specify how the struct will be read from JSON or written in JSON, when the default encoding scheme (i.e. the name of the field) isn't to be used.

Here are a few examples from the json package documentation :

// Field is ignored by this package.
Field int `json:"-"`

// Field appears in JSON as key "myName".
Field int `json:"myName"`

// Field appears in JSON as key "myName" and
// the field is omitted from the object if its value is empty,
// as defined above.
Field int `json:"myName,omitempty"`

// Field appears in JSON as key "Field" (the default), but
// the field is skipped if empty.
// Note the leading comma.
Field int `json:",omitempty"`

答案2

得分: 3

encoding/json中,使用JSON编码/解码的示例:

type TagType struct {
    field1 bool `json:"fieldName"`
    field2 string `json:",omitempty"`
}

更多详细信息请参阅文档:encoding/json

英文:

Example of use is json encoding/decoding in encoding/json:

type TagType struct {
    field1 bool `json:"fieldName"`
    field2 string `json:",omitempty"`
}

More details in documentation: encoding/json

答案3

得分: 0

你也可以使用如下所示的 XML 结构标签:

type SearchResult struct {
    Title  string `xml:"title,attr"`
    Author string `xml:"author,attr"`
    Year   string `xml:"hyr,attr"`
    ID     string `xml:"owi,attr"`
}
英文:

You can also use XML struct tags as shown below

type SearchResult struct {
    Title  string `xml:"title,attr"`
    Author string `xml:"author,attr"`
    Year   string `xml:"hyr,attr"`
    ID     string `xml:"owi,attr"`

}

huangapple
  • 本文由 发表于 2014年4月14日 18:10:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/23057405.html
匿名

发表评论

匿名网友

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

确定