You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.
For some fields, even when omitempty is specified the resulting JSON will still contain empty fields.
It appears there are only two ways to have omitempty work with a struct.
Use a pointer to the struct where the pointer is nil rather than a zero value struct.
Implement Marshaler interface for the surrounding type to provide special handling for zero value structs
The later can be somewhat cumbersome to maintain field parity for large structures. The former means data is no longer contiguous and requires care for multi-threaded use.
The text was updated successfully, but these errors were encountered:
From the documentation:
For some fields, even when
omitempty
is specified the resulting JSON will still contain empty fields.Examples:
Workaround as noted by golang/go#11939 (comment):
The text was updated successfully, but these errors were encountered: