gofmt
What it is
gofmt is the Go source formatter installed with Go. It automatically formats Go programs, using tabs for indentation and blanks for alignment. Without a path it reads standard input; given a directory, it processes .go files recursively while skipping files whose names begin with a period. By default, it writes reformatted source to standard output. Flags can show differences, list files with formatting changes, apply rewrite rules, simplify code, and report all errors. The rewrite option uses patterns and replacements that are valid Go expressions. gofmt also reshapes Go documentation comments into canonical paragraphs, lists, headings, links, and code blocks. The Go project documents integrations for Vim, Emacs, Eclipse, and Sublime Text, along with Git pre-commit hooks and Mercurial. Since Go 1.18, gofmt reads and formats files concurrently, with memory use limited in proportion to GOMAXPROCS. The executable is equivalent to go fmt. It is free, included in Go installations for Windows, macOS, and Linux, and distributed under the Go project's BSD-style license.
For and against
For
- Included with Go installations at no charge
- Formats Go source with consistent indentation and alignment
- Can show diffs or list files needing changes
- Supports rewrite rules and code simplification
- Editor and version-control integrations are documented
Against
- Supports Go source rather than other languages
- Directory processing skips files beginning with a period
The verdict
gofmt provides standard Go formatting from the command line, with options for checking, simplifying, and rewriting source. Its directory behavior skips files whose names start with a period.
Who it suits
gofmt suits Go developers who want a command-line formatter or editor and version-control integrations. Its options also support checking differences and applying expression-based rewrite rules.
Plans and prices
gofmt
included executable in Go installation · command-line use
Read 1 Oct 2026 · go.dev
On the record
- Comment formatting
- Gofmt reformats Go documentation comments into canonical paragraphs, lists, headings, links, and code blocks.go.dev · read 1 Oct 2026
- Distribution
- The Go installation process installs an executable called gofmt, equivalent to go fmt.go.dev · read 1 Oct 2026
- Editor integrations
- The Go project documents gofmt integrations for Vim, Emacs, Eclipse, Sublime Text, Git pre-commit hooks, and Mercurial.go.dev · read 1 Oct 2026
- Formatting options
- Its flags include displaying diffs, listing files with formatting differences, applying rewrite rules, simplifying code, and reporting all errors.go.dev · read 1 Oct 2026
- Input handling
- Without an explicit path, gofmt processes standard input; with a directory, it recursively processes .go files while ignoring files beginning with a period.go.dev · read 1 Oct 2026
- License
- Go is an open source project distributed under a BSD-style license.go.dev · read 1 Oct 2026
- Output
- By default, gofmt prints reformatted sources to standard output.go.dev · read 1 Oct 2026
- Performance
- Since Go 1.18, gofmt reads and formats input files concurrently with memory limited proportionally to GOMAXPROCS.go.dev · read 1 Oct 2026
- Purpose
- Gofmt automatically formats Go programs using tabs for indentation and blanks for alignment.go.dev · read 1 Oct 2026
- Security
- The Go project issues security fixes to the two most recent major Go releases and provides a security policy for reporting vulnerabilities.go.dev · read 1 Oct 2026
- Source transformation
- The -r flag applies rewrite rules whose patterns and replacements are valid Go expressions.go.dev · read 1 Oct 2026
- Support
- Questions about using Go or announcements relevant to Go users should be sent to the golang-nuts mailing list.go.dev · read 1 Oct 2026