Go Reference ยท The go Command and Tools
The go fmt command and gofmt
Reference for go fmt and gofmt: the one standard Go style, flags -l -d -w -s -r, what the formatter changes, import sorting and CI checks.
Go has one official code style, and gofmt applies it. It rewrites source files with canonical indentation (tabs), spacing, alignment of comments and struct fields, and line breaks around blocks. Because every project uses the same formatter, nobody debates style in code review, and diffs show only real changes.
go fmt is a thin wrapper: it runs gofmt -l -w on the packages you name. Use gofmt directly when you need more control, such as listing unformatted files, printing diffs, or simplifying code.
go fmt ./... # rewrite files in place gofmt -l . # list files whose formatting differs gofmt -d main.go # show what would change, as a diff gofmt -s -w . # also apply safe simplifications
Try it
The formatter lives in the standard library as go/format, so you can run exactly what the tool does.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
go fmt [-n] [-x] [packages] gofmt [flags] [path ...]
| gofmt flag | Description |
|---|---|
-l |
List files whose formatting differs from gofmt's. Prints names only. |
-d |
Print a diff instead of rewriting files. |
-w |
Write the result back to the file instead of standard output. |
-s |
Simplify code: []T{T{}} becomes []T{{}}, for _ = range x becomes for range x, s[a:len(s)] becomes s[a:]. |
-r 'a -> b' |
Rewrite rule: apply a pattern replacement to expressions, for example -r 'a[b:len(a)] -> a[b:]'. |
-e |
Report all syntax errors, not just the first ten. |
| go fmt flag | Description |
|---|---|
-n |
Print the gofmt commands that would run, without running them. |
-x |
Print the commands as they run. |
Exit status: gofmt exits nonzero only for syntax errors or I/O problems, not when files need formatting. To fail a build on unformatted files, test whether gofmt -l . prints anything. With no path, gofmt reads standard input and writes standard output, which is how editors call it.
Usage notes
- No options for style. You cannot configure indentation or line length. Tabs are used for indentation and spaces for alignment, always. Gofmt never wraps long lines, so line length is up to you.
go fmthas fewer options thangofmt: it always writes files and cannot simplify. It works on packages (./...), whilegofmtworks on files and directories.- Imports are sorted within each blank-line-separated group, which is why a missing blank line can merge your standard-library and third-party imports. Tools like
goimportsadditionally add and remove imports and group them by origin. - Comments are preserved, and
//go:directives stay attached to their declaration. Since Go 1.19, doc comments get light formatting: lists and headings are normalized, and code blocks are indented with a tab. - Struct fields and comments are aligned into columns. A blank line or a comment line breaks an alignment block, which is a handy way to group related fields.
- Editor integration. Editors run gofmt (or
gopls format) on save, so you rarely call it by hand. In CI, runtest -z "$(gofmt -l .)". - Code that cannot parse is not formatted; gofmt reports the position of the error. Type errors do not matter: gofmt only needs valid syntax.
- Generated code is formatted by tools with
go/format, as in go generate. - Stable output. The formatting is stable across Go versions except for rare, announced changes, so a repository stays clean when the toolchain updates.