Removing fields from struct or hiding them in JSON Response

后端 未结 13 1386
孤街浪徒
孤街浪徒 2020-12-12 09:37

I\'ve created an API in Go that, upon being called, performs a query, creates an instance of a struct, and then encodes that struct as JSON before sending back to the caller

13条回答
  •  [愿得一人]
    2020-12-12 10:27

    Here is how I defined my structure.

    type User struct {
        Username string  `json:"username" bson:"username"`
        Email    string  `json:"email" bson:"email"`
        Password *string `json:"password,omitempty" bson:"password"`
        FullName string  `json:"fullname" bson:"fullname"`
    }
    

    And inside my function set user.Password = nil for not to be Marshalled.

提交回复
热议问题