Golang中奇怪的类型定义语法(名称,然后键入,然后是字符串文字) [英] Strange type definition syntax in Golang (name, then type, then string literal)

查看:204
本文介绍了Golang中奇怪的类型定义语法(名称,然后键入,然后是字符串文字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$ c我一直在试图找出如何使用mgo(MongoDB for Go)的驱动程序,并且遇到了这个结构声明: $ c> type Something struct {
Id bson.ObjectId_id,omitempty
Name string
}

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



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



Go规格中找不到任何内容,我也不知道如何在Google 。 =noreferrer> 语言规范中的Struct types 部分:


字段声明后面可以跟随一个可选的字符串文字
tag ,它成为所有字段的属性相应的字段声明。这些标签通过
反射界面显示,但会被忽略。




  //与TimeStamp协议缓冲区相对应的结构体。 
//标记字符串定义协议缓冲区字段号。
struct {
microsec uint64field 1
serverIP6 uint64field 2
process stringfield 3
}


I've been trying to find out how to use mgo (MongoDB driver for Go) and I came across this struct declaration:

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

I don't quite understand the syntax of the first element (Id). I understand that it's being declared as type bson.ObjectId, but what is the string literal doing there?

My question is not about the mgo driver functionality,
but about this strange <name> <type> <string_literal> syntax.

I couldn't find anything on the Go specs, and I don't know how to google this either.

解决方案

It's explained in the Struct types section of the language specification:

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 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"
}

这篇关于Golang中奇怪的类型定义语法(名称,然后键入,然后是字符串文字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆