Cannot retrieve “_id” value using mgo with golang

前端 未结 3 1044
無奈伤痛
無奈伤痛 2021-01-07 18:14

This is my struct definition:

type Article struct {
    Id      bson.ObjectId `json:\"id\"        bson:\"_id,omitempty\"`
    Title   string        `json:\"t         


        
3条回答
  •  礼貌的吻别
    2021-01-07 18:49

    I've found the problem.

    In the code:

    Id      bson.ObjectId `json:"id"        bson:"_id,omitempty"`
    

    between json: and bson:, I used a tab instead of space so the problem occurs. If I change this line of code to:

    Id      bson.ObjectId `json:"id" bson:"_id,omitempty"`
    

    With one space between json: and bson:, it turns out to work just fine.

提交回复
热议问题