Introduction
Gofumpt is a Go code formatter that builds on top of the standard gofmt with additional opinionated rules. Where gofmt intentionally leaves some formatting decisions open, gofumpt closes those gaps by enforcing stricter conventions, reducing style debates in code reviews and producing more uniform Go codebases.
What Gofumpt Does
- Applies all standard gofmt formatting rules as a baseline
- Removes unnecessary empty lines inside function bodies and blocks
- Standardizes empty declaration blocks (var, const, type) to single-line form
- Groups adjacent same-type declarations and removes redundant parentheses
- Enforces consistent spacing around composite literals and short variable declarations
Architecture Overview
Gofumpt is implemented as a post-processor that first runs the standard gofmt formatting pass, then applies its own additional rules on the resulting AST. This architecture guarantees that gofumpt output is always a superset of gofmt: any file formatted by gofumpt is also valid gofmt output. The extra rules operate on the token-level positions in the AST, adjusting whitespace and newlines without changing semantics.
Self-Hosting & Configuration
- Install with
go install mvdan.cc/gofumpt@latest - Run
gofumpt -w .to format all Go files in place - Use as a drop-in replacement for gofmt in editor save hooks
- Configure VS Code by setting
"gopls": {"gofumpt": true}in settings - Integrates with golangci-lint via the gofumpt linter option
Key Features
- Strict superset of gofmt ensuring full backward compatibility
- Deterministic output eliminates formatting debates in code reviews
- Editor integration through gopls for format-on-save workflows
- CI-friendly with
-d(diff) and-l(list changed files) modes - Actively maintained with rules evolving alongside Go releases
Comparison with Similar Tools
- gofmt — Go's standard formatter; gofumpt adds stricter rules on top
- goimports — manages imports and formats; gofumpt focuses on code body formatting
- golines — wraps long lines; gofumpt handles whitespace and blank lines
- Prettier (JS) — opinionated formatter for JS; gofumpt is the Go equivalent in philosophy
- Revive — lints for style violations; gofumpt automatically fixes formatting issues
FAQ
Q: Is gofumpt output compatible with gofmt? A: Yes, gofumpt is a strict superset. Any file formatted by gofumpt is also valid gofmt output, but not vice versa.
Q: How do I enable gofumpt in VS Code?
A: Add "gopls": {"formatting.gofumpt": true} to your VS Code settings.json and gopls will use gofumpt on save.
Q: Will gofumpt change my code behavior? A: No, gofumpt only modifies whitespace and formatting. It never changes identifiers, logic, or semantics.
Q: Can I use gofumpt alongside golangci-lint? A: Yes, golangci-lint includes gofumpt as a configurable linter that checks and reports formatting differences.