小编典典

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

go

我一直在尝试找出如何使用mgo(Go的MongoDB驱动程序),并且遇到了这个struct声明:

type Something struct {
    Id bson.ObjectId "_id,omitempty"
    Name string
}

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

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

我在Go规格中找不到任何东西,我也不知道该如何在Google上搜索。


阅读 302

收藏
2020-07-02

共1个答案

小编典典

语言规范的“
结构类型”部分对此进行了解释:

字段声明后可以跟一个可选的字符串文字 标签 ,该 标签
成为相应字段声明中所有字段的属性。这些标签通过反射界面可见,
但在其他情况下将被忽略。

// 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"
}
2020-07-02