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



  //与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 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中奇怪的类型定义语法(名称,然后键入,然后是字符串文字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:40