Struct field alignment
Reports Go struct types where reordering fields would reduce memory usage by minimizing padding.
The Go compiler inserts padding bytes between struct fields to satisfy alignment requirements. When fields are ordered suboptimally, this padding can waste significant memory — especially in structs allocated in large numbers (slices, maps, caches).
Reordering fields from largest to smallest alignment eliminates unnecessary padding and reduces the struct's memory footprint. Use the Update struct layout quick-fix to apply the optimal field order automatically.
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.
GoStructLayout- 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.
Example:
Here, bool fields between int64 fields cause 7 bytes of padding each due to alignment requirements. The struct wastes 14 out of 24 bytes (58%).
After the quick-fix is applied:
By placing the largest field first, both bool fields share a single padding block at the end. The struct size drops from 24 to 16 bytes — a 33% reduction.
Inspection ID: GoStructLayout
Inspection options
Here you can find the description of settings available for the Struct field alignment inspection, and the reference of their default values.
- Only highlight if padding exceeds (bytes)
Option ID:
thresholdDefault value:
4
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: |