WhatschatDocsProgramming
Related
How to Build a Natural Language Ads Manager with Claude Code and Spotify's APIPython 3.15.0 Alpha 3 Released: A Developer Preview of Upcoming FeaturesThe Challenge of Bundling Python Applications: A Q&AEverything You Need to Know About the Python Insider Blog's RelocationMastering Python Development: Cursor vs Windsurf – A Comprehensive GuideStructured Prompt-Driven Development: A Team Approach to AI-Assisted CodingHow Go Handles Type Construction and Cycle Detection Behind the ScenesPython 3.15 Alpha 4: Unveiling New Features and Improvements

How to Modernize Your Go Code Using the Source-Level Inliner and //go:fix inline

Last updated: 2026-05-19 02:31:08 · Programming

Introduction

Go 1.26 introduces a powerful new tool for keeping your codebase modern: the source-level inliner combined with the //go:fix inline directive. This feature, part of the redesigned go fix command, allows package authors to define simple API migrations that automatically update client code. Instead of manually tracking deprecations or rewriting calls, you can write an inline directive once and let the tool handle the rest. This guide walks you through the process step by step, from understanding the concept to applying it in your own projects.

How to Modernize Your Go Code Using the Source-Level Inliner and //go:fix inline
Source: blog.golang.org

The source-level inliner works by replacing a function call with a copy of the function's body, substituting arguments for parameters directly in your source files. Unlike the compiler's inliner (which optimizes intermediate representations), this transformation is durable changes the source code permanently. It’s the same engine that powers gopls’ “Inline call” refactoring, and now it’s available as a self-service modernizer in go fix.

What You Need

  • Go 1.26 or later – Download from go.dev/dl.
  • An existing Go project with functions you want to inline or migrate.
  • Basic familiarity with the go fix command.
  • (Optional) gopls for interactive preview – helps verify changes before committing.

Step-by-Step Instructions

Step 1: Understand the Target

Decide which function you want to inline. Typical candidates are small helper functions that have become obsolete or are too trivial to keep as separate calls. For example, a function like func sum(a, b int) int { return a + b } might be better expressed inline if used only once or twice. The inliner works best on functions that are deterministic (no side effects beyond their return value) and have a short body.

Step 2: Write a Fix Directive

In the source file that defines the function you want to inline, add a //go:fix inline comment immediately before the function declaration. This tells go fix that every call to this function should be replaced with its body. The directive can also include an import path if you want to restrict the change to a specific package. Example:

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

You can also specify an optional importpath qualifier to limit inlining to calls from other packages: //go:fix inline mymodule/mypackage. This is useful for package authors rolling out API changes gradually.

Step 3: Run go fix

Open a terminal in your module root and execute:

go fix ./...

This runs all available analyzers, including the source-level inliner. If you only want to apply inline fixes, use:

go fix -fix=inline ./...

The command scans your entire module for calls to functions marked with //go:fix inline and replaces each call with the function body, adjusting variable names to avoid conflicts.

Important: The fix is applied in place. Always commit or backup your code before running go fix. Use version control to review changes.

How to Modernize Your Go Code Using the Source-Level Inliner and //go:fix inline
Source: blog.golang.org

Step 4: Review the Changes

After running go fix, inspect the diff with git diff (or your VCS tool). You’ll see each call site replaced by the body of the function, with arguments substituted. For example, calling sum(3, 4) becomes 3 + 4. The inliner handles complex cases like closures, variadic calls, and renamed parameters safely.

If you use gopls, you can also test the transformation interactively. Place your cursor on a call and invoke “Source Action…” then “Inline call”. This gives you a live preview before committing to a mass update.

Step 5: Test and Commit

Run your tests:

go test ./...

Because the inliner is designed to preserve semantics, tests should pass. However, check for any false positives – the tool does its best but human review is essential. Once satisfied, commit the changes with a message like “api: inline trivial helper functions using go fix”.

Tips for Success

  • Start small – Test the inliner on a single function with few call sites before tackling an entire codebase.
  • Use importpath qualifiers if you are a package maintainer. This allows your users to adopt the migration gradually, one package at a time, without breaking their build.
  • Beware of side effects – The inliner is safe for pure functions. Functions that rely on order of evaluation, closure capture, or non-deterministic behavior should be inlined with caution. Always validate the output.
  • Leverage gopls for learning. The interactive “Inline call” refactoring shows the exact transformation you can expect from //go:fix inline. Use it as a sandbox.
  • Combine with other fix rules – Go 1.26 includes several new modernizers (e.g., for slices/maps functions). You can run them together: go fix -fix=inline,modernize ./....
  • Keep original source until you are confident. A backup branch or tag prevents accidental loss.

The source-level inliner is a game-changer for code maintenance. It empowers both library authors and application developers to modernize APIs effortlessly. By following these steps, you can reduce technical debt and keep your Go code clean and current.