Closed
Description
What version of Go are you using (go version
)?
$ go version go version go1.20 linux/amd64
What did you do?
Marshal a struct into Json with omitempty.
https://go.dev/play/p/nTyS0p_JP3E
type A struct {
AA string `json:"aa,omitempty"`
AB []struct {
ABA int `json:"aba,omitempty"`
ABB string `json:"abb,omitempty"`
} `json:"ab,omitempty"`
AC struct {
ACA int `json:"aca,omitempty"`
} `json:"ac,omitempty"`
}
func main() {
a, b := json.Marshal(A{AA: "Hum"})
fmt.Printf("%s\n%v", a, b)
}
What did you expect to see?
{"aa":"Hum"}
What did you see instead?
{"aa":"Hum","ac":{}}
Activity
zephyrtronium commentedon Mar 21, 2023
See also #11939 and #45669, and tangentially #22480. This is working as currently documented:
With respect to omitempty, there is no "empty" value of any struct type.
IGLOU-EU commentedon Mar 21, 2023
Thank you, that true. I go read their's proposal !