From 539202f32b361523f2a18b1481bcd06a136a3c16 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 26 Dec 2024 18:05:07 -0500 Subject: [PATCH] _content/doc/go1.24: motivate omitzero Change-Id: I4327eff0080bc279bb0c686d3c8266b05af3de95 Reviewed-on: https://go-review.googlesource.com/c/website/+/638564 Reviewed-by: Dmitri Shuralyov Reviewed-by: Dmitri Shuralyov LUCI-TryBot-Result: Go LUCI --- _content/doc/go1.24.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/_content/doc/go1.24.md b/_content/doc/go1.24.md index 169e9268d4..6f78ce3df0 100644 --- a/_content/doc/go1.24.md +++ b/_content/doc/go1.24.md @@ -542,10 +542,15 @@ already implemented `TextMarshaler` and/or `BinaryMarshaler`. #### [`encoding/json`](/pkg/encoding/json/) + When marshaling, a struct field with the new `omitzero` option in the struct field tag will be omitted if its value is zero. If the field type has an `IsZero() bool` method, that will be used to determine whether the value is zero. Otherwise, the value is zero if it is [the zero value for its type](/ref/spec#The_zero_value). +The `omitzero` field tag is clearer and less error-prone than `omitempty` when +the intent is to omit zero values. +In particular, unlike `omitempty`, `omitzero` omits zero-valued +[`time.Time`](/pkg/time#Time) values, which is a common source of friction. If both `omitempty` and `omitzero` are specified, the field will be omitted if the value is either empty or zero (or both).