Formatters
What is gofmt? An introduction to the Go Project's Go formatter
Gofmt is the Go command-line tool that formats Go programs using tabs for indentation and blanks for alignment. It reads source code and prints it reformatted, with flags to list, diff, overwrite, rewrite and simplify. Gofmt is part of the Go standard library.
- Tool
- gofmt
- Made by
- Go Project
- Official site
- pkg.go.dev/cmd/gofmt
- Category
- Formatters
Who makes gofmt
Gofmt is made by the Go Project. The official website is linked below and points to the command's documentation on pkg.go.dev.
Official website: pkg.go.dev/cmd/gofmt
What gofmt is for
Gofmt formats Go programs. Its alignment assumes the editor uses a fixed-width font.
Without a path, it reads standard input. Given a directory, it processes all .go files recursively. By default it prints the reformatted sources to standard output rather than changing files, so you can inspect the result before applying it.
It can also format program fragments from standard input while preserving leading indentation.
Who gofmt is for
The vendor states that gofmt is for Go developers. Anyone writing Go programs can use it to keep source code laid out consistently, whether on a single file or on a whole directory tree.
Notable capabilities
Gofmt's behavior is controlled by a small set of flags, each covering a distinct task.
- The -d flag prints diffs for files whose formatting differs.
- The -l flag lists files whose formatting differs.
- The -w flag overwrites files in place and restores the original from an automatic backup if an error occurs.
- The -r flag applies a rewrite rule to the source before reformatting; rules use valid Go expressions, with single-character lowercase identifiers as wildcards.
- The -s flag simplifies code, for example simplifying s[a:len(s)] to s[a:].
- Formats program fragments from standard input while preserving leading indentation.
- Processes all .go files recursively when given a directory.
How gofmt fits formatters
In the formatters category, gofmt is the Go command-line formatter and is part of the Go standard library, so it is licensed under BSD-3-Clause like the rest of the Go project's code. Its -d and -l flags report which files differ in formatting, and -w rewrites them.
For Linthelm's niche of linters, static analysis and code quality, gofmt covers formatting and, through -s, some code simplification. Two caveats apply: simplifications from -s may be incompatible with earlier Go versions, and the -r implementation is slow.
Frequently asked questions
What is gofmt used for?
Gofmt formats Go programs, using tabs for indentation and blanks for alignment. By default it prints the reformatted sources to standard output.
How do I make gofmt change files in place?
Use the -w flag. It overwrites files in place and restores the original from an automatic backup if an error occurs.
How can I see which files gofmt would change?
Use -l to list files whose formatting differs, or -d to print diffs for those files.
What does the gofmt -s flag do?
It simplifies code, for example turning s[a:len(s)] into s[a:]. These simplifications may be incompatible with earlier Go versions.
Is gofmt part of the Go standard library?
Yes. Gofmt is part of the Go standard library and is licensed under BSD-3-Clause.
Linthelm is independent and is not affiliated with Go Project. Product names belong to their owners. Features change; confirm details on the official website.