当前位置:网站首页>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
边栏推荐
- EfficientNet
- JS string method summary
- STM32编译出现error: L6235E: More than one section matches selector - cannot all be FIRST/L
- Adobe audit prompts that the sampling rate of audio input does not match the output device - problem solving
- 剑指 Offer 笔记: T53 - II. 0~n-1 中缺失的数字
- [unity entry program] creator kitfps: first person shooting 3D game
- Summary of leetcode SQL exercises (MySQL Implementation)
- 广东:剧本杀等新行业新业态场所,消防安全监管不再“缺位”
- 剑指 Offer 笔记: T53 - I. 在排序数组中查找数字
- 求不同采样周期下的传递函数有限零点
猜你喜欢

LNMP架构搭建(部署Discuz论坛)

【产品】关于微信产品分析

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

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

Shell编程之正则表达式(Shell脚本文本三剑客之grep)

Shell脚本文本三剑客之awk

哈希表 详细讲解

STS下载教程(include官网无法下载解决方案)

解决方案:Can not issue executeUpdate() or executeLargeUpdate() for SELECTs

IDEA: Can‘t use Subversion command line client:svn 解决方案
随机推荐
Keil MDK compilation appears..\user\stm32f10x H (428): error: # 67: expected a "}" wrong solution
JS字符串方法总结
【无标题】多模态模型 CLIP
LNMP架构搭建(部署Discuz论坛)
Firewall firewall
origin如何作一张图中多张子图是柱状图(或其他图)
剑指 Offer 笔记: T58 - II. 左旋转字符串
Wilcoxon rank sum and signed rank
Idea: can't use subversion command line client: SVN solution
Interaction free shell programming
暂用 Solo,博客选择困难
【机器学习-白板推导系列】学习笔记---概率图模型和指数族分布
数据库 cli 工具 docker 镜像
go入门篇 (3)
IDEA: Can‘t use Subversion command line client:svn 解决方案
解决方案:idea project没有显示树状图
Newton Raphson iterative method
Sword finger offer notes: t57 - I. and two numbers of S
PWM的原理和PWM波的产生
[unity entry program] creator kitfps: first person shooting 3D game