golang json marshal: how to omit empty nested struct

后端 未结 2 1209
感情败类
感情败类 2020-12-14 14:15

go playground

As shown in the code above, one can use json:\",omitempty\" to omit certain fields in a struct to appear in json.

For example

2条回答
  •  渐次进展
    2020-12-14 14:40

    Easy way

    type  struct {
    < varname > < vartype > \`json : -\`
    }
    

    Example :

    type Boy struct {
    name string \`json : -\`
    }
    

    this way on marshaling name will not get serialized.

提交回复
热议问题