Bvoxro Stack

Go 1.26's Source-Level Inliner: Simplifying API Migrations and Code Modernization

Go 1.26's new go fix includes a source-level inliner that transforms API migrations and refactoring. Learn how it works and how to use it.

Bvoxro Stack · 2026-05-15 01:05:18 · Programming

Introduction

Go 1.26 introduces a completely revamped go fix command, designed to help developers keep their codebases current and maintainable. Among its new features is a powerful source-level inliner—a tool that not only enhances interactive refactoring but also enables package authors to create their own API migration rules. This article explores what the source-level inliner is, how it works, and why it’s a game-changer for Go development.

Go 1.26's Source-Level Inliner: Simplifying API Migrations and Code Modernization
Source: blog.golang.org

What Is Source-Level Inlining?

Inlining a function call means replacing the call with a copy of the called function’s body, after substituting actual arguments for formal parameters. Traditional compilers perform this transformation on their internal representations to generate faster code—this is compiler inlining. In contrast, source-level inlining modifies the actual source code permanently, rewriting the file to remove the function call and insert the inlined logic.

Go’s source-level inliner was originally built in 2023 and has since become a cornerstone of several code transformation tools. If you’ve used the “Inline call” refactoring in gopls (for example, in VS Code under “Source Action…”), you’ve already witnessed it in action. The before-and-after view clearly shows a call like sum(a, b) replaced by the expression a + b (assuming sum was a trivial addition function).

How the Inliner Works in Practice

The inliner must handle a variety of subtle correctness issues. For instance, it needs to correctly rename variables to avoid shadowing, handle side effects from argument evaluation, and preserve the order of evaluation. It also ensures that return statements within the inlined function are properly translated. Because of these complexities, the inliner is not just a simple text replacement—it operates on Go’s abstract syntax tree (AST) and applies a series of semantic transformations.

Beyond the “Inline call” command, gopls uses the same inliner for other refactorings like “Change signature” and “Remove unused parameter”. When a function signature changes, the inliner can eliminate old call sites by expanding them in place, making the migration seamless and safe.

Go 1.26's Source-Level Inliner: Simplifying API Migrations and Code Modernization
Source: blog.golang.org

The Role in go fix and Self-Service Migrations

The new go fix incorporates the source-level inliner as one of its built-in analyzers. This allows package maintainers to define self-service API migrations without writing custom refactoring tools. By specifying simple rewrite rules, authors can automatically update all usages of a deprecated function to its modern replacement.

For example, imagine a library that renames OldFunc to NewFunc with identical behavior. A rule can instruct go fix to inline all calls to OldFunc and replace them with the equivalent NewFunc calls. The inliner ensures that arguments and return values are handled correctly, and the result is a source-level transformation that users can commit immediately.

This approach drastically reduces the burden of migrating large codebases: instead of manually updating hundreds of call sites, developers run a single go fix command. Moreover, because the inliner works on the AST, it can handle complex cases like function literals, generics, and closures.

Conclusion

Go 1.26’s source-level inliner is a versatile tool that bridges the gap between compiler optimizations and day-to-day code maintenance. Whether you’re performing quick interactive refactorings in your editor or rolling out a major API change across your organization, the inliner provides a safe, automated way to modernize your Go code. To learn more, check out the official Go blog post and start experimenting with go fix today.

Recommended