Mastering //go:fix inline and the Source-Level Inliner in Go 1.26

By

Introduction

The source-level inliner introduced in Go 1.26 transforms how you modernize and refactor Go code. Unlike traditional compiler inlining that works on ephemeral intermediate representations, this tool makes durable changes to your source files. Integrated into both the go fix command and the gopls editor assistant, it allows you to replace function calls with the actual function body, substituting arguments for parameters. This guide will walk you through using //go:fix inline and the source-level inliner to automate API migrations, simplify debugging, and keep your codebase clean.

Mastering //go:fix inline and the Source-Level Inliner in Go 1.26
Source: blog.golang.org

What You Need

Step-by-Step Guide

Step 1: Upgrade to Go 1.26

First, ensure you have Go 1.26 installed. Check your current version with go version. If it’s older, download and install the latest from the official site. Verify after installation:

go version

You should see go1.26 or later.

Step 2: Understand the //go:fix inline Directive

The directive //go:fix inline is a code comment you place above a function signature. It tells the go fix tool that calls to this function should be inlined wherever they appear. For example:

//go:fix inline
func sum(a, b int) int {
    return a + b
}

When you later run go fix, every call to sum(x, y) will be replaced by x + y. This is especially useful for migrating callers to a new API while keeping the transition safe and automated.

Step 3: Apply Inlining with go fix

Navigate to your module root and run:

go fix

By default, go fix applies all registered modernizers, including the source-level inliner. To restrict to only the inliner, use the -fix flag:

go fix -fix='go:fix/inline'

The tool will modify your source files in place. For an inlined function like sum, a call six() that contained return sum(3, 3) becomes return 3 + 3. The original function definition is removed if no remaining calls exist, or kept otherwise.

Step 4: Interactive Inlining with gopls

If you prefer interactive refactoring, install or launch your editor with gopls support. In VS Code, open a Go file, place the cursor on a function call, open the Command Palette (Ctrl+Shift+P), search for “Source Action”, and choose “Inline call”. The editor shows a realistic preview and applies the transformation.

Mastering //go:fix inline and the Source-Level Inliner in Go 1.26
Source: blog.golang.org

This uses the same underlying algorithm as go fix but gives you control over each change.

Step 5: Create Custom Modernizers for Your Own Package

As a package author, you can help users of your library keep their code up-to-date. Simply add //go:fix inline comments above deprecated or trivial functions. After users upgrade to Go 1.26, they can run go fix to automatically inline all such calls, removing dependency on old versions and improving performance. You can also combine this with other fix directives like //go:fix rewrite for more complex migrations.

Test your directives locally by running go fix -fix 'go:fix/inline' ./... on a small project that uses your package.

Tips for Success

By mastering //go:fix inline and the source-level inliner, you can keep your Go codebase modern with minimal manual effort. Embrace automated refactoring and let the tool do the heavy lifting.

Tags:

Related Articles

Recommended

Discover More

Mastering Modern CSS: A Hands-On Guide to Clip-Path Jigsaws, View Transitions, Scoping, and BeyondPreparing Your JetBrains Plugin for Remote DevelopmentWarming Seas Threaten Seagrass Meadows: New Study Reveals Which Habitats May SurvivePacific Northwest's Hidden Danger: The Earth's Crust Is Tearing Apart Beneath UsGermany Exposes REvil and GandCrab Mastermind: Russian Daniil Shchukin Named as 'UNKN'