Golang中的奇怪类型定义语法(名称,然后是类型,然后是字符串文字)

Seb*_*oli 32 go

我一直试图找出如何使用mgo(Go的MongoDB驱动程序),我遇到了这个结构声明:

type Something struct {
    Id bson.ObjectId "_id,omitempty"
    Name string
}
Run Code Online (Sandbox Code Playgroud)

我不太了解第一个元素(Id)的语法.我知道它被声明为类型bson.ObjectId,但字符串文字在那里做什么?

我的问题不是关于mgo驱动程序的功能,
而是关于这种奇怪的<name> <type> <string_literal>语法.

我在Go规格上找不到任何东西,我也不知道如何谷歌这个.

Kei*_*son 45

它在语言规范Struct types部分中进行了解释:

字段声明后面可以跟一个可选的字符串文字 标记,该标记成为相应字段声明中所有字段的属性.标签通过反射界面可见, 但否则将被忽略.

// 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"
}
Run Code Online (Sandbox Code Playgroud)