Skip to content

Commit

Permalink
_content/doc/go1.24: motivate omitzero
Browse files Browse the repository at this point in the history
Change-Id: I4327eff0080bc279bb0c686d3c8266b05af3de95
Reviewed-on: https://go-review.googlesource.com/c/website/+/638564
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
aclements committed Dec 27, 2024
1 parent b27bc76 commit 539202f
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions _content/doc/go1.24.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,15 @@ already implemented `TextMarshaler` and/or `BinaryMarshaler`.

#### [`encoding/json`](/pkg/encoding/json/)

<!-- go.dev/issue/45669 -->
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).
Expand Down

0 comments on commit 539202f

Please sign in to comment.