# Struct optimization

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/help/go/2026.3/llms.txt).

> **Note:**
> The Graph tab in a pprof profile requires the bundled Diagrams plugin. If the tab shows a plugin requirement message, open settings by pressing `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Comma` (VSCode), `⌘ Comma` (VSCode (macOS)) and navigate to `Plugins | Installed`.  Enable the Diagrams plugin and restart the IDE if prompted.

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:

```GO
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:

```GO
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.

Procedure: 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](https://resources.jetbrains.com/help/img/idea/2026.3/go_struct_optimization_analysis.png)

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.

Procedure: 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.

Procedure: Enable inspections from the IDE settings

Open settings by pressing `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Comma` (VSCode), `⌘ Comma` (VSCode (macOS)) 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](https://resources.jetbrains.com/help/img/idea/2026.3/go_struct_optimization_highlighting.png)

## 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](https://resources.jetbrains.com/help/img/idea/2026.3/go_struct_optimization_results.png)

## 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.

Procedure: Apply a quick-fix in the editor

1. Place the caret on the highlighted struct declaration.

2. Press `Alt+Enter` (Windows), `⌥ ⏎` (macOS), `⌥ ⏎` (IntelliJ IDEA Classic (macOS)), `⌥ ⏎` (macOS System Shortcuts), `Alt+Enter` (XWin), `Alt+Enter` (GNOME), `Alt+Enter` (KDE), `Alt+Enter` (Emacs), `Alt+Enter` (Sublime Text), `⌥ ⏎` (Sublime Text (macOS)), `Ctrl+.` (VSCode), `⌘ .` (VSCode (macOS)).

3. Select Update struct '`StructName`' layout.

![Apply a quick-fix for struct optimization](https://resources.jetbrains.com/help/img/idea/2026.3/go_struct_optimization_quick_fix.png)

Procedure: 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.

