gofmt
Format Go source code
TLDR
Format a file and display the result to the console
Format a file, overwriting the original file in-place
Format a file, and then simplify the code, overwriting the original file
Print all (including spurious) errors
SYNOPSIS
gofmt [flags] [path ...]
gofmt [flags] [file ...]
When no path or file is given, gofmt processes the standard input.
PARAMETERS
-l
List files whose formatting differs from gofmt's output. Does not write to files.
-w
Write result to source file instead of standard output. This is the most common and recommended flag for modifying files.
-r rule
Apply rewrite rules to the source code. Rules are of the form 'pattern -> replacement' and are applied as Go AST transformations.
-s
Simplify code. This flag is enabled by default when using -r, applying common Go idiom simplifications.
-d
Display diffs of the formatting changes on standard output. The output format is similar to diff -u.
-e
Report all parsing errors, not just the first one encountered during processing.
-cpuprofile file
Write a CPU profile to the specified file, useful for debugging performance issues with gofmt itself.
-v
Print verbose information, including details about the files and directories being processed.
-local prefix
Only consider packages whose import path begins with prefix to be local to the current module or project.
DESCRIPTION
gofmt is a fundamental tool within the Go programming language ecosystem that automatically formats Go source code according to the official Go programming language style.
It operates by parsing the Go source code into an abstract syntax tree (AST) and then printing that AST back into source code format following a predefined set of formatting rules.
This process ensures that all Go code formatted by gofmt adheres to a single, canonical style, promoting universal readability and consistency across different projects and developers.
It's an essential part of the Go development workflow, often integrated into IDEs, build systems, or pre-commit hooks, effectively eliminating stylistic disagreements and allowing developers to focus purely on the code's logic rather than its aesthetic presentation.
CAVEATS
gofmt enforces a strict, opinionated style with no configurable options for customization (e.g., indent size, brace style).
This deliberate design choice is intended to eliminate stylistic debates but can be a point of adjustment for developers accustomed to highly configurable code formatters.
USAGE WITH THE <B>GO</B> COMMAND
While gofmt can be run directly, it is most frequently invoked via the go fmt subcommand.
go fmt automatically applies gofmt to all Go source files within the current module or specified packages, making it a convenient wrapper.
DETERMINISTIC OUTPUT
A fundamental characteristic of gofmt is its deterministic output. For any given Go source input, it will always produce the exact same formatted output, irrespective of the operating system, environment, or previous formatting.
This property is crucial for automated tooling, continuous integration, and maintaining consistent codebases without unnecessary diffs.
HISTORY
gofmt was developed as an integral part of the Go programming language project, conceived by Rob Pike.
It was released with the initial public release of Go (Go 1.0) in March 2012, though its core principles and implementation were present in early Go development.
Its creation was motivated by a strong desire to eliminate style debates and ensure that all Go code is consistently readable across the entire ecosystem, fostering a uniform appearance that significantly aids in code comprehension and review processes.