Mahdi.Kh
September 21, 2026
We learn how go fix helps us analyze and improve our code after upgrading to newer versions of Golang.
The go fix tool uses a set of predefined rules to identify parts of the code that can be improved and automatically rewrites them. This tool usually converts older code into a more up-to-date form so it can take advantage of newer Go language features.
The go fix command can be used together with a set of patterns (similar to go build) that specify which files it should modify.
Running the command above runs go fix on every file in the current directory or its subdirectories. If no problems or errors occur, the parts of the code that can be improved are identified and automatically rewritten. Files that were generated automatically (see the go generate part) are ignored.
It's recommended that after every time you upgrade your Go version, you run the go fix command on your project once, so that the parts of the code that can be identified are rewritten according to the newly introduced features.
To see the changes before they're applied, you can use the -diff flag. This way you can review the changes before they're applied.
To see the list of analyzers available in the go fix command, we can use go tool fix help.
go tool fix help commandBy adding the analyzer's name to the command, we can see its full documentation:
go tool fix forvar commandBy default, when we use the go fix command, all analyzers run on the code. To run only the analyzers we want, we simply pass the analyzer's name as a flag to the command. For example, go fix -forvar ./… runs only the forvar analyzer on the code.
Conversely, if we want to run all analyzers except a specific set, we simply pass the names of the analyzers we want disabled as flags and set them to false.
If your code has tags or files that differ based on CPU type or platform, you can restrict the go fix command's run to a specific platform and CPU type by specifying GOOS and GOARCH.
This way, by restricting changes by analyzer, platform, and CPU type, you can apply changes gradually and in smaller chunks, instead of suddenly facing hundreds of changes across hundreds of files, applying the changes in several stages but in smaller sizes.
Previous part: The sync Standard Package | Golang basics