golang json mardshal忽略字段和默认零值
type User struct {
Name string `json:"name"`
Email string `json:"email,omitempty"`
Age int `json: "age"`
Addess string `json:"-"`
Amount int `json:"amount"`
smallCount int `json:"smallCount"`
}
func TestIgnore(tt *testing.T) {
u1 := User{
Name: "asong",
Age: 18,
Addess: "beijing",
smallCount: 20,
}
b, err := json.Marshal(u1)
if err != nil {
fmt.Printf("json.Marshal failed, err:%v\n", err)
return
}
fmt.Printf("str:%s\n", b)
//str:{"name":"asong","Age":18,"amount":0}
}