Go结构体字段的第三个参数是什么?

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

What is the third parameter of a Go struct field?

问题

json:"commitIndex" 是一个结构体字段的标签(tag),用于指定在将结构体转换为 JSON 格式时,该字段应该使用的名称。在这个例子中,CommitIndex 字段在转换为 JSON 时将使用名称 commitIndex。标签是一种元数据,用于为字段提供额外的信息或指令。在这种情况下,json:"commitIndex" 标签告诉编码器在生成 JSON 时使用指定的字段名称。

英文:
type Config struct {
	CommitIndex uint64 `json:"commitIndex"`
	// TODO decide what we need to store in peer struct
	Peers []*Peer `json:"peers"`
}

I understand what the first two columns are,but what is json:"commitIndex"?

答案1

得分: 47

这被称为结构体标签,可以在运行时使用reflect包进行解析。

来自https://golang.org/ref/spec#Struct_types:

字段声明后可以跟一个可选的字符串字面值标签,该标签成为相应字段声明中所有字段的属性。

这些标签通过反射接口可见,并参与结构体的类型标识,但在其他情况下被忽略。

一些使用反射的包,如jsonxml,使用标签来更好地处理特殊情况。

英文:

It's called a struct tag, they can be parsed using the reflect package at runtime.

From https://golang.org/ref/spec#Struct_types:

>A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration.
>
> The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored.

Some packages that use reflection like json and xml use tags to handle special cases better.

答案2

得分: 22

你所指的是标签,**Go规范**中有如下说明:

> 字段声明后可以跟一个可选的字符串字面值标签,该标签将成为相应字段声明中所有字段的属性。标签通过反射接口可见,并参与结构体的类型标识,但在其他情况下会被忽略。

// 对应于TimeStamp协议缓冲区的结构体。
// 标签字符串定义了协议缓冲区字段的编号。
struct {
    microsec  uint64 "field 1"
    serverIP6 uint64 "field 2"
    process   string "field 3"
}

这在编译时不起作用,但在运行时通过不同的包对结构体进行反射时会被使用。正如Amit已经指出的那样,encoding/json包使用它来指定编组/解组行为。encoding/xmlgopkg.in/mgo.v2/bson等也是如此。

按照约定,标签字符串是由空格分隔的字符串。如**reflect**中所述:

> 按照约定,标签字符串是可选的以空格分隔的key:"value"对的串联。每个key是一个非空字符串,由非控制字符组成,除了空格(U+0020 ' ')、引号(U+0022 '"')和冒号(U+003A ':')。每个value使用U+0022 '"'字符进行引用,并使用Go字符串字面值语法。

英文:

What you are referring to is called a tag, and the Go specification states:

> A field declaration may be followed by an optional string literal tag,
> which becomes an attribute for all the fields in the corresponding
> field declaration. The tags are made visible through a reflection
> interface and take part in type identity for structs but are otherwise
> ignored.

// A struct corresponding to the TimeStamp protocol buffer.
// The tag strings define the protocol buffer field numbers.
struct {
	microsec  uint64 "field 1"
	serverIP6 uint64 "field 2"
	process   string "field 3"
}

This does nothing at compile time, but is used by different packages when doing runtime reflection on the struct. As Amit already pointed out, the encoding/json package is using it to specify marshalling/unmarshalling behaviour. The same goes with encoding/xml, gopkg.in/mgo.v2/bson, etc.

The tag string is by convention a space separated string. As stated in the reflect package:

> By convention, tag strings are a concatenation of optionally
> space-separated key:"value" pairs. Each key is a non-empty string
> consisting of non-control characters other than space (U+0020 ' '),
> quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using
> U+0022 '"' characters and Go string literal syntax.

huangapple
  • 本文由 发表于 2014年8月26日 11:05:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/25497375.html
匿名

发表评论

匿名网友

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

确定