当前位置:网站首页>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
边栏推荐
- JS字符串方法总结
- 源码编译安装LAMP
- [unity entry program] creator kitfps: first person shooting 3D game
- Shell script text three swordsmen sed
- Sword finger offer note: T39. Numbers that appear more than half of the time in the array
- 【无标题】多模态模型 CLIP
- How to make a graph? Multiple subgraphs in a graph are histogram (or other graphs)
- 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)
- V-show失效问题
- A possibility that ch340 module cannot be recognized / burned
猜你喜欢

origin如何作一张图中多张子图是柱状图(或其他图)

EfficientNet

为什么TCP三次握手的时候ACK=Seq+1

Maker harmony OS application development training notes 01

N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei

Adobe Audition提示 音频输入的采样率与输出设备不匹配——问题解决

Solution of digital tube flash back after proteus8 professional version cracking

makefile模板

JS-寄生组合式继承

【机器学习-白板推导系列】学习笔记---条件随机场
随机推荐
Principle of control system based on feedback rate
go入门篇 (4)
1.Flume 简介及基本使用
数据库 cli 工具 docker 镜像
Arduino常见供电问题与解决
Sword finger offer note: t45. arrange the array into the smallest number
Open source Flink has datastream connector written with holo or Flink SQL Conn
compute_class_weight() takes 1 positional argument but 3 were given
Proteus8专业版破解后用数码管闪退的解决
剑指 Offer 笔记: T58 - II. 左旋转字符串
剑指 Offer 笔记: T57 - II. 和为 s 的连续正数序列
Some commonly used shortcut keys for MathType
Adobe Audition提示 音频输入的采样率与输出设备不匹配——问题解决
JS-寄生组合式继承
你真的会写二分查找吗——变种二分查找
Idea: can't use subversion command line client: SVN solution
LAN SDN hard core technology insider 23 looking forward to the future - RDMA (Part 1)
The first case of monkeypox in pregnant women in the United States: the newborn was injected with immunoglobulin and was safely born
LAN SDN hard core technology insider 25 looking forward to the future - RDMA (Part 2)
iptables防火墙