go fmt: Usage, Options & Common CI Errors
Apply the canonical Go formatting.
go fmt runs gofmt on the named packages and rewrites files in place. Because go fmt always writes, CI formatting checks use gofmt -l (list) or gofmt -d (diff) instead of go fmt.
What it does
go fmt ./... resolves packages and runs gofmt -l -w on their files, reformatting them to the canonical style. The underlying gofmt tool offers read-only modes that go fmt does not expose.
Common usage
Terminal
go fmt ./... # format all packages in place
gofmt -l . # list files that need formatting
gofmt -d . # show the formatting diff
test -z "$(gofmt -l .)" # CI gate: fail if any unformattedCommon CI error: using go fmt as a check
A pipeline runs go fmt ./... expecting it to fail on unformatted code, but go fmt rewrites files and exits 0, so it never gates anything. Fix: gate with gofmt: test -z "$(gofmt -l .)" exits non-zero when any file is unformatted, listing the offenders.
Options
| Command | Effect |
|---|---|
| go fmt ./... | Format in place (always writes) |
| gofmt -l . | List unformatted files |
| gofmt -d . | Print formatting diffs |
| gofmt -w . | Write changes |
Related guides
go vet: Usage, Options & Common CI Errorsgo vet reports suspicious constructs that compile but are likely bugs. Learn how to vet all packages and fix…
go build: Usage, Options & Common CI Errorsgo build compiles Go packages and binaries. Learn -o, ./..., cross-compiling with GOOS/GOARCH, and how to fix…
cargo fmt: Usage, Options & Common CI Errorscargo fmt formats Rust with rustfmt. Learn the --check flag for CI, how to format the whole workspace, and ho…