Strange type definition syntax in Golang (name, then type, then string literal)

前端 未结 1 373
故里飘歌
故里飘歌 2020-11-30 07:41

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          


        
相关标签:
1条回答
  • 2020-11-30 07:51

    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"
    }
    
    0 讨论(0)
提交回复
热议问题