Code Formatting and Linting Tools
Covers the standard Go formatting and linting tools such as gofmt, goimports, go vet, staticcheck, and golangci-lint, why they exist, and how to use them
Go has a strong opinion about how code should look. The language ships with tools that automatically enforce a single canonical style, and the ecosystem provides a rich set of linters that catch bugs and suspicious patterns before they reach production. This page covers the tools you need to keep Go code clean, consistent, and correct.
Why Formatting and Linting Matter
In most programming languages, teams spend time debating indentation, brace placement, and import ordering. Go eliminates those debates entirely: there is one correct format, defined by the gofmt tool, and the community treats any deviation as simply wrong. This saves time and makes all Go code look familiar.
Linting takes this further. A linter is a program that examines source code for potential problems without actually executing it. In Go, linters can detect unused variables, code that will never run, incorrect use of formatting verbs, and much more. While the compiler already rejects many mistakes, linters catch a class of errors the compiler silently accepts, subtle bugs that are easy to miss in code review.
gofmt and go fmt
gofmt reads Go source code and outputs the same program with standard formatting. It does not change the logic; it only adjusts spacing, indentation, line breaks, and alignment according to the official Go style.
The command go fmt is a wrapper around gofmt that applies it to all the Go files in the packages you specify. Running go fmt ./... at the root of a module will reformat every .go file in the module.
Mechanically, gofmt parses Go source into an abstract syntax tree (AST) and then pretty-prints that AST with the canonical rules. Because it works on the AST level, it can handle even extremely messy code and produce clean, readable output.
Using gofmt Directly
# Show the diff between the file and what gofmt would produce
gofmt -d main.go
# Write the formatted version back to the file
gofmt -w main.go
# List files that are not formatted according to gofmt
gofmt -l .
When gofmt -l . prints nothing, every file already matches the standard format.
All Formatted:
If gofmt -l produces no output, your files are already correctly formatted. This is the goal before every commit.
go fmt in Practice
go fmt accepts the same package patterns as other go commands. It calls gofmt -l -w on the matching files, so it both formats and reports which files it touched.
go fmt ./...
This is the command you are most likely to run by hand or wire into a pre-commit hook.
How Beginners Should Think About gofmt
Think of gofmt as a spell-checker for code appearance. You write the logic; the tool makes sure the presentation is correct. Once you get used to it, you stop worrying about where to put spaces and braces, and your editor can run it automatically on every save.
A Common Mistake with gofmt
Running gofmt on Broken Code:
gofmt needs valid Go syntax. If your file has a syntax error, gofmt will fail with a parse error and will not format the file. Fix the syntax error first.
A real-world example: this snippet has inconsistent indentation and a missing import for fmt.
package main
func main() {
fmt.Println("hello")
x:= 42
print(x)
}
Running gofmt -d main.go shows what would change:
diff main.go.orig main.go
--- main.go.orig
+++ main.go
@@ -1,6 +1,7 @@
package main
+
func main() {
-fmt.Println("hello")
-x:= 42
- print(x)
+ fmt.Println("hello")
+ x := 42
+ print(x)
}
Look closely at what changed and what did not. gofmt fixed the indentation, added spaces around :=, and put a blank line after the package clause. It did not add the missing import "fmt", even though the file cannot compile without it. That is not a bug: gofmt never touches imports. It only adjusts spacing and layout. The file still fails to build after formatting, which is exactly why the next tool exists.
goimports
goimports is a superset of gofmt that also manages import statements. It adds missing imports, removes unused ones, and organizes them into standard library and external groups.
It was created because gofmt alone does not touch imports at all. A file with a missing "fmt" import and a stray "net/http" that is never used will still be syntactically valid, so gofmt leaves it alone. goimports fixes that.
Install it:
go install golang.org/x/tools/cmd/goimports@latest
It accepts the same flags as gofmt: -w writes back, -d shows a diff, -l lists files that need changes.
Take the same messy file from the gofmt section above. Running goimports -d main.go on it produces a longer diff than gofmt did, because now the import appears too:
diff -u main.go.orig main.go
--- main.go.orig
+++ main.go
@@ -1,6 +1,9 @@
package main
+
+import "fmt"
+
func main() {
-fmt.Println("hello")
-x:= 42
- print(x)
+ fmt.Println("hello")
+ x := 42
+ print(x)
}
The +import "fmt" lines are the difference between the two tools. Everything else in the diff, the indentation and the spacing, is the shared gofmt behaviour underneath. goimports does everything gofmt does, plus import management.
The same applies in the other direction. Given a file that imports a package it never uses:
// main.go before goimports
package main
import "net/http"
func main() {
fmt.Println("hello")
}
goimports -d main.go outputs a diff that removes "net/http" and adds "fmt". After formatting, the import block is clean and only contains what the code actually uses.
Run goimports inside a module:
For goimports to resolve third-party imports, it needs the module context, so run it inside a directory that belongs to a module (one with a go.mod file). Standard library imports like "fmt" resolve anywhere, but a package from another module cannot be found without it.
goimports is the tool most developers configure their editor to run on save, because it ensures the import block never drifts out of sync with the code.
go vet
go vet is an official linter that ships with Go. It checks for common mistakes that the compiler does not flag, such as:
- Using a
fmt.Printfverb that does not match the argument type - Struct field tags with wrong syntax
- Unreachable code
- Passing
testing.T(or other testing structures) incorrectly - Misuse of
sync/atomicfunctions
Run it with:
go vet ./...
If the command prints nothing, no issues were found.
Consider this example, saved as vet.go:
package main
import "fmt"
func main() {
name := "world"
fmt.Printf("hello %d\n", name) // should be %s
}
The compiler accepts this, but go vet reports:
vet.go:7:20: fmt.Printf format %d has arg name of wrong type string
Read the location as file, line, column: the Printf call sits on line 7, and column 20 is where the mismatched argument name starts. The check is conservative: it only flags constructs that are almost certainly wrong, based on the vet checks bundled with the standard distribution. It will not catch unused variables, unreachable code buried deeper in logic, or style issues.
Not a Complete Solution:
go vet is designed to run quickly and with zero configuration, but it catches only a subset of issues. For complete static analysis, combine it with staticcheck or golangci-lint.
staticcheck
staticcheck is a useful static analysis tool that catches a much wider range of problems than go vet. It detects:
- Unused functions, variables, and fields
- Code that always returns the same value (simplifiable expressions)
- Deprecated standard library functions
- Deferred calls to
Closethat discard errors - Many more patterns cataloged at staticcheck.io
Install:
go install honnef.co/go/tools/cmd/staticcheck@latest
Run it against a package:
staticcheck ./...
Take this code, saved as main.go in a module, a helper function that nothing ever calls:
package main
import "fmt"
func helper() string {
return "never called"
}
func main() {
fmt.Println("hello")
}
staticcheck outputs:
main.go:5:6: func helper is unused (U1000)
The location points at the function declaration itself, line 5, column 6, where the name helper starts. The diagnostic code (U1000) names a specific check, which you can look up to understand why it fires. Note that this is a different class of finding from a compile error: the program builds and runs fine with the dead function in it, so neither the compiler nor go vet complains. staticcheck is the tool that notices.
Standalone or Aggregated:
staticcheck can be run directly, but it is also one of the default linters inside golangci-lint. Running it through golangci-lint gives you caching and parallel execution.
golangci-lint
golangci-lint is a meta-linter that runs dozens of individual linters at once, including staticcheck, go vet, errcheck, and many more. It was built for speed: it runs linters concurrently and caches results so that subsequent runs are nearly instant.
Install version 2 with:
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.15.0
The project recommends installing a released binary over go install for reproducibility, but the go install path above works and puts the binary in your GOPATH/bin. The old install path without /v2/ installs the version 1 line, which reached its final release in February 2025 and receives no further updates, so do not use it for new setups.
A typical invocation:
golangci-lint run ./...
That command checks all Go files in the module with a sensible default set of linters. If you need to tune which linters are enabled or adjust their severity, create a .golangci.yml configuration file. Version 2 configuration requires a version: "2" key, and formatters live under their own section instead of under linters. A minimal example:
version: "2"
linters:
enable:
- govet
- staticcheck
- errcheck
- gosec
formatters:
enable:
- gofmt
- goimports
golangci-lint will use this file automatically when it is present in the project root. In version 1, gofmt and goimports were listed under linters.enable; in version 2 they are formatters, and putting them under linters no longer works. To run only the formatters, use the separate golangci-lint fmt command.
Running golangci-lint run ./... in the module with the unused helper function from the staticcheck section above reports the same problem, this time under the unused linter name:
main.go:5:6: func helper is unused (unused)
func helper() string {
^
1 issues:
* unused: 1
Ignoring Linter Warnings:
It is tempting to disable linters or add //nolint comments when a check is annoying. Each suppression should be reviewed like any other code change, the linter is often right, and disabling it hides a real problem.
The tool is designed for CI pipelines. A typical GitHub Actions step runs golangci-lint run and fails the build on any warning. Because of its speed and ease of configuration, golangci-lint has become the standard linting tool in most professional Go projects.
Editor Integration
While this chapter focuses on the command-line tools, they are most effective when integrated into your editor. The Go language server gopls already runs many of these checks automatically. Most editor plugins also provide a "format on save" hook that invokes goimports.
For example, in VS Code with the Go extension, enabling the following settings makes formatting and linting transparent:
"editor.formatOnSave": true,
"go.formatTool": "goimports",
"go.lintTool": "golangci-lint"
The key takeaway is that these tools are not meant to be run manually every time. They belong in your save hook, your pre-commit hook, and your CI pipeline.
Summary
The Go ecosystem ships with a canonical formatter (gofmt) and a lint foundation (go vet), but the community has built a broader suite of tools that make code cleaner and safer. goimports takes care of imports; staticcheck catches deep logic mistakes; and golangci-lint ties everything together with a single, fast command.
The workflow that emerges is: write code, let your editor format it on save with goimports, and let golangci-lint catch problems before you push. If you adopt these habits early, you will never waste time on style discussions or debugging errors a linter could have caught.