Gofmt
gofmt is Go’s standard source-code formatter, applying the language’s canonical formatting style automatically.
Go in Practice, Second Edition
go fmt is a Go command that reformats Go source files according to the language's canonical formatting style.
- How it is used
- Keeps shared Go source consistently formatted as part of everyday development.
- How deep it goes
- Shows a before-and-after diff and a preview command, while leaving some newline and import-declaration choices to developers.
- Examples
- Uses a poorly formatted Go file and a `gofmt -d` diff showing whitespace, indentation, and semicolon changes.
- What you can do after
- Operate/configure it by running `go fmt`, previewing changes with `gofmt -d`, or enabling IDE formatting.
Go in Action, Second Edition
gofmt is the standard Go source-code formatter that applies the official Go formatting style.
- How it is used
- Standardizes Go source layout so developers can focus reviews and collaboration on behavior rather than formatting preferences.
- How deep it goes
- Explains parsing, rewriting, formatting modes, and editor use, but does not descend into formatter implementation internals.
- Examples
- Demonstrates `gofmt -d main.go`, its sample diff, and `gofmt -w main.go` for in-place rewriting.
- What you can do after
- Operate/configure it via `gofmt -d` for inspection and `gofmt -w` for direct file rewriting.
Shipping Go: Develop, deliver, discuss, design, and go again
The standard Go source-code formatter that applies the language's canonical formatting conventions.
- How it is used
- Standardizes Go source formatting and turns style consistency into an automated merge check.
- How deep it goes
- Explains visible formatting changes and CI enforcement, but does not detail formatter internals.
- Examples
- Uses go fmt ./..., a Makefile check-format target, and a pull-request format-check job.
- What you can do after
- Operate/configure gofmt locally and enforce clean formatting through a CI merge gate.
Read how Shipping Go: Develop, deliver, discuss, design, and go again explains gofmt →
Related concepts
- goimportsused by · goimports is a Go source-code formatter that formats code and adds, removes, or organizes import declarations based on usage.
- formattingimplements · Formatting is the arrangement or transformation of content, data, or code according to specified structural, stylistic, or presentation conventions.
- Goconfigures · Go is a statically typed, compiled, garbage-collected programming language with built-in support for concurrency.
- go commandpart of · The go command is Go’s standard command-line interface for compiling, building, testing, formatting, analyzing, and managing Go programs and dependencies.
- style guideimplements · A style guide is a documented set of conventions for consistently formatting, naming, organizing, or presenting source code.
- build taguses · A build tag is a Go source-file constraint that determines whether a file is included in a particular build configuration.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


