Overview
Gofmt enforces a standard formatting style across all Go code, eliminating debates about code style and ensuring consistency. Most Go code in the wild is formatted with gofmt.Using gofmt is considered a best practice in the Go community. Most editors can be configured to run gofmt automatically on save.
Usage
.go files in that directory, recursively.
Flags
Output Flags
Print diffs to standard output instead of reformatted sources. Shows what changes would be made.
List files whose formatting differs from gofmt’s. Does not print reformatted sources.
Write result to source file instead of standard output. Overwrites files in-place with gofmt’s version.
Code Transformation Flags
Apply the rewrite rule to source before reformatting.Format:
pattern -> replacementBoth pattern and replacement must be valid Go expressions.Simplify code (after applying rewrite rule, if any). Applies various simplifications to the code.
Other Flags
Print all errors (including spurious ones).
Write CPU profile to the specified file (for debugging gofmt performance).
Common Usage Patterns
Check Formatting
List all files that need formatting:This is useful in CI/CD pipelines to verify code is formatted.
Format Files
Format all Go files in current directory and subdirectories:The
-w flag writes changes directly to files.Examples
Basic Formatting
Rewrite Rules
The-r flag enables powerful code transformations using pattern matching:
In rewrite patterns, single-character lowercase identifiers (like
α, β, a, b) serve as wildcards matching arbitrary sub-expressions.Simplification with -s
The-s flag applies various simplifications:
Integration with Editors
VS Code
The Go extension automatically runs gofmt on save. Configuration insettings.json:
Vim/Neovim
Using vim-go plugin:Emacs
CI/CD Integration
Checking Formatting in CI
go fmt vs gofmt
Thego fmt command is a wrapper around gofmt:
| Command | Description |
|---|---|
gofmt | Standalone tool, formats files or directories |
go fmt | Wrapper that runs gofmt -l -w on packages |
Formatting Standards
What gofmt Does
- Uses tabs for indentation
- Uses blanks for alignment
- Removes trailing whitespace
- Ensures consistent spacing around operators
- Formats comments properly
- Aligns struct field declarations
- Formats function parameters consistently
What gofmt Doesn’t Do
- Doesn’t enforce line length limits
- Doesn’t reorganize imports (use
goimportsfor that) - Doesn’t add or remove blank lines between functions
- Doesn’t rename variables or functions
Advanced Usage
Formatting Fragments
Gofmt can format Go fragments (not just complete programs):Processing Multiple Files
Performance
Gofmt is designed to be fast:- Typically processes thousands of lines per second
- Can be parallelized across multiple files
- Uses efficient parsing and formatting algorithms
Troubleshooting
Common Issues
Gofmt changes file but editor shows different formatting
Gofmt changes file but editor shows different formatting
Your editor may be using a different formatter (like
goimports or gofumpt). Check your editor’s Go extension settings.Gofmt doesn't fix all style issues
Gofmt doesn't fix all style issues
Gofmt only handles formatting, not style issues. Use
go vet and linters like golangci-lint for additional checks.Files with syntax errors aren't formatted
Files with syntax errors aren't formatted
Gofmt requires valid Go syntax. Fix syntax errors first, then run gofmt.
See Also
go Command
Overview of the go command
Building Programs
Building Go applications