LinuxCommandLibrary

go-fix

Update Go code to newer version

TLDR

Update packages to use new APIs

$ go fix [packages]
copy

SYNOPSIS

go fix [-r rewriter] [-diff] [-force] [build flags] [packages]

PARAMETERS

-diff
    Print diffs instead of rewriting packages

-force
    Apply fixes even if typechecking fails

-r rewriter
    Comma-separated list of rewriters to run (default: all)

DESCRIPTION

The go fix command runs the Go fix tool on specified packages, automatically updating source code to conform to changes in newer Go versions. It applies predefined rewrite rules (rewriters) that address syntax or semantic shifts between releases, such as changes to standard library APIs or language constructs.

Without arguments, it processes all packages in the current directory and subdirectories. It's particularly useful after upgrading Go, ensuring code compatibility without manual edits. Many common fixes are now applied automatically during go build or go get in modern versions (Go 1.17+), reducing the need for explicit invocation.

Fixes are idempotent where possible, meaning running it multiple times won't alter already-fixed code. It respects go.mod and build constraints.

CAVEATS

Many fixes are automatic in Go 1.17+ during builds; go fix may be redundant. Does not fix all issues—manual intervention sometimes needed. Rewriters are Go-version specific.

EXAMPLE

go fix ./...
Runs on all packages in workspace.

go fix -r http.ResponseWriter.WriteHeader -diff
Shows diff for specific rewriter.

EXIT STATUS

0 if successful, 1 if unchanged or errors, 2 if fix applied but issues remain.

HISTORY

Introduced in Go 1.0 alongside the gofix tool for early API changes. Evolved with per-release rewriters (e.g., for io/ioutil removal in Go 1.16). Usage declined post-Go 1.17 as fixes integrated into toolchain.

SEE ALSO

go(1), go fmt(1), go vet(1)

Copied to clipboard