omitempty tag 的作用
作者:互联网
转自:https://blog.csdn.net/stpeace/article/details/82744364 (仅做个人备份,浏览请看原文
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)
}
Addr 字段赋值为“”时,也不返回
标签:p1,Addr,err,Age,json,tag,omitempty,作用,Name 来源: https://blog.csdn.net/chushoufengli/article/details/104757758