4750
Programming

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

Go 1.26 Ships Rewritten go fix for Instant Code Modernization

The Go team has released version 1.26 today, featuring a fully rewritten go fix subcommand. This new tool automatically identifies and applies code improvements, leveraging modern language and library features to streamline maintenance.

Go 1.26 Launches Rewritten `go fix` Command to Modernize Code Automatically
Source: blog.golang.org

"The rewritten go fix 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."

How to Use the New go fix

Simply run go fix ./... 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.

To preview changes before applying, use the -diff flag: go fix -diff ./.... This shows a unified diff of each file, making code reviews easier.

Available Fixers

Run go tool fix help to see the full list of registered analyzers. Key examples include:

  • any: replaces interface{} with any
  • buildtag: checks //go:build and // +build directives
  • forvar: removes redundant re-declaration of loop variables
  • mapsloop: converts explicit map loops to maps package calls
  • minmax: replaces if/else with built-in min and max

Each fixer has detailed documentation accessible via go tool fix help <analyzer>.

Background

go fix 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.

Go 1.26 Launches Rewritten `go fix` Command to Modernize Code Automatically
Source: blog.golang.org

Donovan explains: "We redesigned go fix 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.

What This Means

For developers, this means a dramatic reduction in manual refactoring. Running go fix after each Go upgrade keeps code aligned with best practices without tedious work.

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

Start from a clean Git state before running go fix to ensure the resulting commit only contains the tool's changes. This practice simplifies code review and rollbacks.

Future Direction

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.

For more details, see the official Go blog post.

💬 Comments ↑ Share ☆ Save