当前位置:网站首页>The use of omitempty in go
The use of omitempty in go
2022-07-27 11:57:00 【Big leaves are not small】
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)
}
result :
{"name":"taoge","age":30}
taoge 30
{"name":"Cang Laoshi","age":18,"addr":"Japan"}
Cang Laoshi 18 Japan
You can see , With omitempty after , If addr It's empty , Then it generates json There is no addr Field .
Can be removed omitempty, Try again. .
Not much to say .
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Tao song is still 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/stpeace/article/details/82744364
边栏推荐
- JS parasitic combinatorial inheritance
- SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
- 配置更改删除了路由过滤器,分布路由器不堪重负:加拿大网络大瘫痪
- 53 亿 BI 市场 TOP 10:帆软、微软、永洪、SAP、百度、IBM、SAS、思迈特、Salesforce、浪潮通软
- Solution of digital tube flash back after proteus8 professional version cracking
- Database cli tool docker image
- 你尚未连接代理服务器可能有问题或地址不正确(如何查看代理服务器ip)
- 硬刚甲方后:中国移动 4908 万大单被废
- SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
- 系统临时文件的写和读:createTempFile和tempFileContent[通俗易懂]
猜你喜欢

Principle, concept and construction process of MySQL database master-slave replication cluster

The first case of monkeypox in pregnant women in the United States: the newborn was injected with immunoglobulin and was safely born

Principle of PWM and generation of PWM wave

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

Shell script text three swordsman awk

TLC549Proteus仿真&Sallen-Key滤波器&AD736Vrms到DC转换&Proteus查看51寄存器值

Keil MDK compilation appears..\user\stm32f10x H (428): error: # 67: expected a "}" wrong solution

Shell script text three swordsmen sed

Detailed explanation of hash table

【产品】关于微信产品分析
随机推荐
SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
Sword finger offer notes: t58 - ii Rotate string left
【机器学习-白板推导系列】学习笔记---条件随机场
剑指 Offer 笔记: T45. 把数组排成最小的数
iptables防火墙
w.r.t. ; i.e.; etc.; e. G. what does it mean
Iptables firewall
希腊字母读法
基于bolt数据库实现简单的区块链 day(2)
Arduino常见供电问题与解决
日本福岛废堆安全监视协议会认可排海计划“安全”
[machine learning whiteboard derivation series] learning notes - support vector machine and principal component analysis
CH340模块无法识别/烧写不进的一种可能性
Sword finger offer notes: T53 - ii Missing numbers from 0 to n-1
TLC549Proteus仿真&Sallen-Key滤波器&AD736Vrms到DC转换&Proteus查看51寄存器值
Shell编程之正则表达式(Shell脚本文本三剑客之grep)
Greek alphabet reading
剑指 Offer 笔记: T53 - II. 0~n-1 中缺失的数字
Firewalld防火墙
Beyond compare 3 next difference segment / down search arrow not found