gofmt is the standard source-code formatter supplied with Go. It gives Go projects a consistent layout without requiring developers to agree on large sets of formatting options.
It can format source supplied through standard input, individual files or complete directory trees. Output can be written back to files, compared as a diff or simply printed to standard output.
gofmt also provides source simplification and expression rewriting facilities which go beyond basic whitespace formatting.
This is free and open source software.
Key Features
- Applies the standard Go source-code formatting conventions.
- Processes standard input, individual files and directories recursively.
- Can overwrite source files with their formatted versions.
- Lists files whose formatting differs from gofmt output.
- Produces unified diffs without modifying source files.
- Supports pattern-based expression rewriting with the
-roption. - Can simplify supported Go constructs with the
-soption. - Accepts syntactically valid Go fragments as well as complete programs when reading standard input.
- Preserves appropriate leading indentation when formatting source fragments.
Website: pkg.go.dev/cmd/gofmt
Support:
Developer: The Go Authors
License: BSD 3-Clause License
gofmt is written in Go. Learn Go with our recommended free books and free tutorials.
Related Software
| Go Linters | |
|---|---|
| revive | Designed as a drop-in replacement for golint |
| golangci-lint | Fast Go linters runner |
| gosec | Security scanner for Go projects |
| Staticcheck | Advanced Go linter |
| go-critic | Opinionated Go source code linter |
| Vet | Examines Go source code and reports suspicious constructs |
| gofumpt | Stricter gofmt |
| gofmt | Formats Go programs |
| goconst | Find repeated Go literals that could be replaced with constants |
| go-ruleguard | Analysis-based Go linter that runs dynamically loaded rules |
| sloglint | Enforce consistent structured logging conventions in Go |
| forbidigo | Go linter for forbidding identifiers |
| godot | Keep source code comments consistent |
| Godoc-Lint | Enforce consistent documentation comments in Go code |
| exhaustive | Check Go switch statements for missing enum cases |
Read our verdict in the software roundup.
| Go Code Formatters | |
|---|---|
| goimports-reviser | Sorts and groups imports by standard, third-party and project packages |
| gofumpt | Enforces stricter styling rules for cleaner, consistent source |
| gofmt | Applies standardised indentation, spacing and source layout |
| golines | Breaks lengthy statements into readable, shorter lines |
| goimports | Automatically adds missing imports and removes unused ones |
| gocondense | Combines multiline constructs to reduce unnecessary vertical space |
Read our verdict in the software roundup.
LinuxLinks helps you discover the best free and open source software for Linux. Explore our extensive collection of recommended applications, including alternatives to popular proprietary software.We also publish software reviews, Linux tutorials, hardware reviews, free programming books and articles covering the wider open source community. Browse our Linux resources to discover more. Know a useful free and open source Linux application we haven't covered? Suggest a program for inclusion. Has your software been featured on LinuxLinks? Display one of our Featured on LinuxLinks badges on your website or GitHub README. |


Please read our Comment Policy before commenting.