码迷,mamicode.com
首页 > Web开发 > 详细

go json 转换忽略字段、控制字段可有可无

时间:2020-03-14 11:18:06      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:alt   empty   tput   com   inf   input   temp   使用   ima   

使用场景:在 go 中给 API 调用者响应 json 数据。

  1. 有些字段时不暴露给用户。

  2. 有些字段是根据用户的级别控制是否有这些数据。

如下图:

  1. Id 字段不暴露给用户,则使用 `json:"-"` 修饰。

  2. Inputs、Outputs 在某些情况下不返回字段数据。(1)、使用 `json:"omitempty"`(当字段为空时忽略此字段) 修饰字段;(2)、当不需要该字段返回时,让其赋值为空即可

 

技术图片

go json 转换忽略字段、控制字段可有可无

标签:alt   empty   tput   com   inf   input   temp   使用   ima   

原文地址:https://www.cnblogs.com/dfsxh/p/12490767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!