Using go fix to modernize Go code

Using go fix to modernize Go code – The Go Programming Language The Go Blog Using go fix to modernize Go code Alan Donovan 17 February 2026 The 1.26 release of Go this month includes a completely rewritten go fix subcommand. Go fix uses a suite of algorithms to identify opportunities to improve your code, often by taking advantage of more modern features of the language and library. In this post, we’ll first show you how to use go fix to modernize your Go codebase. Then in the second section we’ll dive into the infrastructure behind it and how it is evolving. Finally, we’ll present the theme of “self-service” analysis tools to help module maintainers and organizations encode their own guidelines and best practices. Running go fix The go fix command, like go build and go vet , accepts a set of patterns that denote packages. This command fixes all packages beneath the current directory: $ go fix ./… On success, it silently updates your source files. It discards any fix that touches generated files since the appropriate fix in that case is to the logic of the generator itself. We recommend running go fix over your project each time you update your build to a newer Go toolchain release. Since the command may fix hundreds of files, start from a clean git state so that the change consists only of edits from go fix; your code reviewers will thank you. To preview the changes the above command would have made, use the -diff flag: $ go fix -diff ./… — dir/file.go (old) +++ dir/file.go (new) – eq := strings.IndexByte(pair, ‘=’) – result[pair[:eq]] = pair[1+eq:] + before, after, _ := strings.Cut(pair, “=”) + result[before] = after … You can list the available fixers by running this command: $ go tool fix help … Registered analyzers: any replace interface{} with any buildtag check //go:build and // +build directives fmtappendf replace []byte(fmt.Sprintf) with fmt.Appendf forvar remove redundant re-declaration of loop variables hostport check format of addresses passed to net.Dial inline apply fixes based on ‘go:fix inline’ comment directives mapsloop replace explicit loops over maps with calls to maps package minmax replace if/else statements with calls to min or max … Adding the name of a particular analyzer shows its complete documentation: $ go tool fix help forvar forvar: remove redundant re-declaration of loop variables The forvar analyzer removes unnecessary shadowing of loop variables. Before Go 1.22, it was common to write `for _, x := range s { x := x … }` to create a fresh variable for each iteration. Go 1.22 changed the semantics of `for` loops, making this pattern redundant. This analyzer removes the unnecessary `x := x` statement. This fix only applies to `range` loops. By default, the go fix command runs all analyzers. When fixing a large project it may reduce the burden of code review if you apply fixes from the most prolific analyzers as separate code changes. To enable only specific analyzers, use the flags matching their names. For e

Source: Hacker News | Original Link