'omitempty' tag on a struct field can be changed
Reports JSON struct tags that use ,omitempty on struct-typed fields.
For struct values, ,omitempty does not omit the field, because a struct value is never nil.
In Go 1.24 and later, ,omitzero omits the field when the struct value is the zero value.
Note: Replacing ,omitempty with ,omitzero is a behavior change. The field may be omitted in cases where it was previously always present.
Example:
To fix the code, do one of the following:
Use the Remove redundant omitempty tag quick-fix to keep the current JSON output.
Use the Replace omitempty with omitzero (behavior change) quick-fix to omit the field when the struct value is zero.
After the quick-fix that removes the tag:
Locating this inspection
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
GoFixOmitZero- Via Settings dialog
Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.
Inspection ID: GoFixOmitZero
Suppressing Inspection
You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:
More detailed instructions as well as other ways and options that you have can be found in the product documentation:
Inspection Details | |
|---|---|
By default bundled with: |