当前位置:网站首页>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
边栏推荐
- 剑指 Offer 笔记: T58 - II. 左旋转字符串
- torch‘ has no attribute ‘inference_mode‘
- 【机器学习-白板推导系列】学习笔记---条件随机场
- Detailed explanation of hash table
- Sword finger offer notes: T53 - ii Missing numbers from 0 to n-1
- VSCode复制代码时去掉样式/语法高亮/代码高亮/黑色背景
- Several banks adjusted the redemption rules of cash management financial products: the confirmation time limit of redemption changed from "t+0" to "t+1"
- CTF crypto RSA getting started
- w.r.t. ; i.e.; etc.; e. G. what does it mean
- Could not load dynamic library ‘libcudnn.so.8‘;
猜你喜欢

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

makefile模板
![[unity entry program] creator kitfps: first person shooting 3D game](/img/2b/78b535973b2898f53752ceeb25ef01.png)
[unity entry program] creator kitfps: first person shooting 3D game

你真的会写二分查找吗——变种二分查找

求不同采样周期下的传递函数有限零点

Maker harmony OS application development training notes 01

Shell脚本文本三剑客之sed

Regular expression of shell programming (grep of shell script text three swordsmen)

Could not load dynamic library ‘libcudnn.so.8‘;

Could not load dynamic library ‘libcudnn.so.8‘;
随机推荐
Proteus8专业版破解后用数码管闪退的解决
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)
微博评论爬虫+可视化
剑指 Offer 笔记: T53 - I. 在排序数组中查找数字
Shell编程之正则表达式(Shell脚本文本三剑客之grep)
LAN SDN hard core technology insider 24 outlook for the future - RDMA (middle)
Principle of control system based on feedback rate
剑指 Offer 笔记: T53 - II. 0~n-1 中缺失的数字
Vscode removes style / syntax highlighting / code highlighting / black background when copying code
The first case of monkeypox in pregnant women in the United States: the newborn was injected with immunoglobulin and was safely born
JS-寄生组合式继承
Can you really write binary search - variant binary search
TapNet: Multivariate Time Series Classification with Attentional Prototypical Network
SQL statement learning and the use of pymysql
剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
Sword finger offer note: t45. arrange the array into the smallest number
Modelarts image classification and object detection
LeetCode 04: T26. 删除排序数组中的重复项(简单); 剑指 Offer 67. 把字符串转换成整数(中等); 面试题 01.08. 零矩阵 (简单)
Matlab draws Bode diagram with time delay system
Difference quotient approximation of wechat quotient