Skip to content
Latchkey

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 unformatted

Common 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

CommandEffect
go fmt ./...Format in place (always writes)
gofmt -l .List unformatted files
gofmt -d .Print formatting diffs
gofmt -w .Write changes

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →