Go 1.26 Launches Rewritten `go fix` Command to Modernize Code Automatically

<h2>Go 1.26 Ships Rewritten <code>go fix</code> for Instant Code Modernization</h2> <p>The Go team has released version 1.26 today, featuring a fully rewritten <code>go fix</code> subcommand. This new tool automatically identifies and applies code improvements, leveraging modern language and library features to streamline maintenance.</p><figure style="margin:20px 0"><img src="gofix-analysis-facts.svg" alt="Go 1.26 Launches Rewritten `go fix` Command to Modernize Code Automatically" style="width:100%;height:auto;border-radius:8px" loading="lazy"><figcaption style="font-size:12px;color:#666;margin-top:5px">Source: blog.golang.org</figcaption></figure> <p>"The rewritten <code>go fix</code> represents a significant leap forward in code modernization," says Alan Donovan, a Go team member and author of the release blog. "Developers can now instantly upgrade their codebases to use idiomatic patterns with just one command."</p> <h3>How to Use the New <code>go fix</code></h3> <p>Simply run <code>go fix ./...</code> to apply all available fixes to all packages below the current directory. The command silently updates source files on success, skipping any that are machine-generated.</p> <p>To preview changes before applying, use the <code>-diff</code> flag: <code>go fix -diff ./...</code>. This shows a unified diff of each file, making code reviews easier.</p> <h3>Available Fixers</h3> <p>Run <code>go tool fix help</code> to see the full list of registered analyzers. Key examples include:</p> <ul> <li><strong>any</strong>: replaces <code>interface{}</code> with <code>any</code></li> <li><strong>buildtag</strong>: checks <code>//go:build</code> and <code>// +build</code> directives</li> <li><strong>forvar</strong>: removes redundant re-declaration of loop variables</li> <li><strong>mapsloop</strong>: converts explicit map loops to <code>maps</code> package calls</li> <li><strong>minmax</strong>: replaces <code>if/else</code> with built-in <code>min</code> and <code>max</code></li> </ul> <p>Each fixer has detailed documentation accessible via <code>go tool fix help &lt;analyzer&gt;</code>.</p> <h2 id="background">Background</h2> <p><code>go fix</code> has been part of Go since the early days, but the 1.26 version is a complete rewrite. The original tool focused on migrating deprecated APIs; the new version uses a suite of algorithms to detect opportunities for modernization.</p><figure style="margin:20px 0"><img src="https://go.dev/images/google-white.png" alt="Go 1.26 Launches Rewritten `go fix` Command to Modernize Code Automatically" style="width:100%;height:auto;border-radius:8px" loading="lazy"><figcaption style="font-size:12px;color:#666;margin-top:5px">Source: blog.golang.org</figcaption></figure> <p>Donovan explains: "We redesigned <code>go fix</code> to be extensible and self-documenting, enabling team maintainers to create custom fixers for their own codebase guidelines." The new architecture separates analysis from fixing, making it easier to add new rules.</p> <h2>What This Means</h2> <p>For developers, this means a dramatic reduction in manual refactoring. Running <code>go fix</code> after each Go upgrade keeps code aligned with best practices without tedious work.</p> <p>"Module maintainers can now encode their organization's coding standards as reusable fixers, turning best practices into automated chores," says Donovan. The tool also respects generated files automatically, avoiding false positives.</p> <p>Start from a clean Git state before running <code>go fix</code> to ensure the resulting commit only contains the tool's changes. This practice simplifies code review and rollbacks.</p> <h3>Future Direction</h3> <p>The Go team plans to expand the fixer library in subsequent releases. Community contributions for new analyzers are encouraged, following the patterns established in this release.</p> <p>For more details, see the official <a href="https://go.dev/blog/go1.26-fix">Go blog post</a>.</p>
Tags: