Golang mgo getting empty objects

前端 未结 1 1260
情话喂你
情话喂你 2020-12-04 02:28

I\'m trying to learn Go API development. I have a MongoDB instance running in a Docker container. I\'m trying to follow a few guides but am failing on simple queries. I don\

相关标签:
1条回答
  • 2020-12-04 03:11

    You must export fields of structs, else they are ignored by the mgo package. Change fields of users to User and Data.

    type users struct {
        User string `bson:"user" json:"user"`
        Data string `bson:"data" json:"data"` 
    }
    

    By default when a struct value is transformed / stored / retrieved from MongoDB, the field name is used. If you want to use different names, you may use tags to tell what names should the fields map to.

    0 讨论(0)
提交回复
热议问题