Proposal Details
Since #45669 was accepted, for consistency, we should probably support omitzero option in encoding/xml with the exact semantic.
See comments https://github.com/golang/go/issues/45669#issuecomment-2289770548, https://github.com/golang/go/issues/45669#issuecomment-2315878346.
Comment From: callthingsoff
could I gently nudge this proposal for consideration? It makes sense that we can use this option in both encoding/json and encoding/xml packages.
Comment From: aclements
This proposal has been added to the active column of the proposals project and will now be reviewed at the weekly proposal review meetings. — aclements for the proposal review group
Comment From: aclements
Have all remaining concerns about this proposal been addressed?
The proposal is to add support of "omitzero" tag to encoding/xml, with the same semantics of the tag for encoding/json. Specifically,
When marshaling, the "omitzero" option specifies that the struct field should be omitted if the field value is zero as determined by the "IsZero() bool" method if present, otherwise based on whether the field is the zero Go value (according to reflect.Value.IsZero). This option has no effect when unmarshaling. If "omitempty" is specified together with "omitzero", whether a field is omitted is based on the logical OR of the two.
This will mean that omitzero of a slice omits a nil slice but emits [] for a zero-length non-nil slice (and similar for maps). It will also mean that omitzero of a time.Time omits time.Time{}. Neither of these strictly requires calling the IsZero method, but custom types may may find implementing IsZero useful.
Comment From: aclements
Based on the discussion above, this proposal seems like a likely accept. — aclements for the proposal review group
The proposal is to add support of "omitzero" tag to encoding/xml, with the same semantics of the tag for encoding/json. Specifically,
When marshaling, the "omitzero" option specifies that the struct field should be omitted if the field value is zero as determined by the "IsZero() bool" method if present, otherwise based on whether the field is the zero Go value (according to reflect.Value.IsZero). This option has no effect when unmarshaling. If "omitempty" is specified together with "omitzero", whether a field is omitted is based on the logical OR of the two.
This will mean that omitzero of a slice omits a nil slice but emits [] for a zero-length non-nil slice (and similar for maps). It will also mean that omitzero of a time.Time omits time.Time{}. Neither of these strictly requires calling the IsZero method, but custom types may may find implementing IsZero useful.
Comment From: aclements
No change in consensus, so accepted. 🎉 This issue now tracks the work of implementing the proposal. — aclements for the proposal review group
The proposal is to add support of "omitzero" tag to encoding/xml, with the same semantics of the tag for encoding/json. Specifically,
When marshaling, the "omitzero" option specifies that the struct field should be omitted if the field value is zero as determined by the "IsZero() bool" method if present, otherwise based on whether the field is the zero Go value (according to reflect.Value.IsZero). This option has no effect when unmarshaling. If "omitempty" is specified together with "omitzero", whether a field is omitted is based on the logical OR of the two.
This will mean that omitzero of a slice omits a nil slice but emits [] for a zero-length non-nil slice (and similar for maps). It will also mean that omitzero of a time.Time omits time.Time{}. Neither of these strictly requires calling the IsZero method, but custom types may may find implementing IsZero useful.