当前位置:网站首页>omitempty在go中的使用
omitempty在go中的使用
2022-07-27 11:03:00 【大叶子不小】
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Addr string `json:"addr,omitempty"`
}
func main() {
p1 := Person{
Name: "taoge",
Age: 30,
}
data, err := json.Marshal(p1)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", data)
fmt.Println(p1.Name, p1.Age, p1.Addr)
p2 := Person{
Name: "Cang Laoshi",
Age: 18,
Addr: "Japan",
}
data2, err := json.Marshal(p2)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", data2)
fmt.Println(p2.Name, p2.Age, p2.Addr)
}
结果:
{"name":"taoge","age":30}
taoge 30
{"name":"Cang Laoshi","age":18,"addr":"Japan"}
Cang Laoshi 18 Japan
可以看到,有了omitempty后,如果addr为空, 则生成的json中没有addr字段。
可以去掉omitempty, 再试试。
不多说。
————————————————
版权声明:本文为CSDN博主「涛歌依旧」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/stpeace/article/details/82744364
边栏推荐
- [untitled] multimodal model clip
- 1. Introduction and basic use of flume
- The first case of monkeypox in pregnant women in the United States: the newborn was injected with immunoglobulin and was safely born
- Maker Hongmeng application development training notes 03
- Shell脚本文本三剑客之awk
- Vscode removes style / syntax highlighting / code highlighting / black background when copying code
- npm踩坑
- Weibo comment crawler + visualization
- LAN SDN hard core technology insider 24 outlook for the future - RDMA (middle)
- Tlc549proteus simulation &sallen key filter &ad736vrms to DC conversion &proteus view 51 register value
猜你喜欢

美现首例孕妇猴痘病例:新生儿被注射免疫球蛋白,已安全出生

Modelarts voice detection and text classification

Vscode removes style / syntax highlighting / code highlighting / black background when copying code

Beyond compare 3 next difference segment / down search arrow not found

哈希表 详细讲解

【机器学习-白板推导系列】学习笔记---支持向量机和主成分分析法

Keil MDK编译出现..\USER\stm32f10x.h(428): error: #67: expected a “}“错误的解决办法

iptables防火墙

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network
![[untitled] multimodal model clip](/img/f0/8ae72ae0845372b6fe2866fae83f52.png)
[untitled] multimodal model clip
随机推荐
USB 网卡驱动数据流
剑指 Offer 笔记: T39. 数组中出现次数超过一半的数字
Weibo comment crawler + visualization
Detailed explanation of hash table
【机器学习-白板推导系列】学习笔记---支持向量机和主成分分析法
82. (cesium home) cesium points move on 3D models
Japan Fukushima waste dump safety monitoring agreement will recognize the "safety" of the sea discharge plan
Maker Hongmeng application development training notes 02
82.(cesium之家)cesium点在3d模型上运动
[machine learning whiteboard derivation series] learning notes --- conditional random fields
TapNet: Multivariate Time Series Classification with Attentional Prototypical Network
Database cli tool docker image
LAN SDN technology hard core insider 11 the key of cloud convergence CP -- hierarchical port binding
局域网SDN技术硬核内幕 11 云网融合CP的关键——层次化端口绑定
torch‘ has no attribute ‘inference_ mode‘
Leetcode 04: T26. Delete duplicate items in the sorting array (simple); Sword finger offer 67. convert the string to an integer (medium); Interview question 01.08. zero matrix (simple)
Finding the finite zero point of transfer function under different sampling periods
Newton-Raphson迭代法
数据包传输:应用层-内核-硬件
求不同采样周期下的传递函数有限零点