GoLand 2026.2 Help

Struct optimization

The Struct optimization tool helps you detect inefficient struct layouts and reduce memory usage in Go applications.

In Go, the order of struct fields affects memory alignment. A suboptimal field order can introduce extra padding bytes and increase the total size of a struct.

For example, the following struct layout is inefficient:

type Inefficient struct { A byte // 1 byte B int32 // 4 bytes C byte // 1 byte }

The struct is laid out in memory as follows:

  • Field A occupies 1 byte.

  • The next 3 bytes are padding to align field B to a 4-byte boundary.

  • Field B occupies 4 bytes.

  • Field C occupies 1 byte.

  • Another 3 bytes of padding are added so that the total struct size matches the largest alignment requirement.

As a result, the struct occupies 12 bytes in memory even though its fields require only 6 bytes.

A more efficient layout groups larger fields first:

type Efficient struct { B int32 // 4 bytes A byte // 1 byte C byte // 1 byte }

This layout reduces padding and decreases the total struct size.

GoLand detects inefficient struct layouts and suggests optimized field ordering. This helps reduce the memory footprint without changing program behavior.

Run struct optimization analysis

You can analyze a file, uncommitted files, a custom scope, or the whole project.

Run struct optimization analysis

  1. Select View | Tool Windows | Go Optimization from the main menu.

  2. Open the Go Optimization tool window.

  3. Select Struct optimization.

  4. Choose the analysis scope:

    • Whole project

    • Uncommitted files

    • File

    • Custom scope

    Struct optimization analysis
  5. Click Analyze.

GoLand will then highlight inefficient struct layouts directly in the editor and provide quick-fixes.

Enable struct optimization inspections

You can enable struct layout inspections directly in the editor to detect inefficient layouts while writing code.

Enable inspections from the Go Optimization tool window

  1. Select View | Tool Windows | Go Optimization from the main menu.

  2. Open the Go Optimization tool window.

  3. Select Struct optimization.

  4. Enable Enable 'Struct optimizations' inspection by default.

Enable inspections from the IDE settings

Open settings by pressing Ctrl+Alt+S and navigate to Editor | Inspections.

  1. Expand Go | General | Struct field alignment.

  2. Enable the inspection checkbox.

  3. Optional: configure the inspection threshold in Only highlight if padding exceeds (bytes).

GoLand will then highlight inefficient struct layouts directly in the editor and provide quick-fixes.

Struct optimization highlighting

Review optimization suggestions

When GoLand detects an inefficient struct layout, it reports the amount of wasted memory and shows an optimized field order.

The inspection results include:

  • the current struct layout

  • padding bytes added for alignment

  • the optimized layout

  • estimated memory savings

Select an inspection result to compare the current and optimized layouts.

Struct optimization results

Apply the optimization

After GoLand detects an inefficient struct layout, you can apply the suggested optimization either directly in the editor or from the inspection results.

Apply a quick-fix in the editor

  1. Place the caret on the highlighted struct declaration.

  2. Press Alt+Enter.

  3. Select Update struct 'StructName' layout.

    Apply a quick-fix for struct optimization

Apply optimization from inspection results

  1. Select an inspection result in the Inspection Results tool window.

  2. Review the proposed field order.

  3. Click Update.

13 July 2026