gofmt & goimports - Small Tools, Big Difference
Because formatting shouldn’t be a team debate

Software Engineer x Data Engineer - I make the world a better place to live with software that enables data-driven decision-making
Search for a command to run...
Because formatting shouldn’t be a team debate

Software Engineer x Data Engineer - I make the world a better place to live with software that enables data-driven decision-making
No comments yet. Be the first to comment.
In this series, I will discuss the Go linters, use cases, how-tos and everything related
Crash course in fixing a "gosec" Golang linter issue
Why fanning out OAuth introspection inside your VPC creates a distributed bottleneck - and how to keep internal auth fast.

How Model Context Protocol and Agent Communication Protocol shape the AI stack.

A practical follow-up on turning agent instructions into focused, testable workflows.

How to reduce context size, improve reasoning, and build more predictable AI coding workflows

A simple habit that improves code reviews, code quality, and team productivity

If you’re in Go more than hour, you’ve probably noticed the mention about the gofmt. It’s one of those tools that cleans up your mess - without a redundant style discussion.
Next to it there’s a little bit more clever sibling - goimports. At first glance it’s doing the same, but in practice it’s saving you a lot of time and frustration.
gofmt it’s an official Go tool for the code formatting according to the language’s standards.
There’s no configuration, there’s no Google or Uber style, there’s just a Go style.
# One file formatting
gofmt -w main.go
# Directory formatting (recursively)
gofmt -w .
The -w flag means that the file will be overwritten with the gofmt's version if its styling is different.
To see the list of files that do not follow fhe gofmt's formatting you use the -l flag:
gofmt -l .
goimports is a tool from the golang.org/x/tools/cmd/goimports package that can do everything what gofmt does and as a bonus automatically manages package imports.
Command goimports updates your Go import lines, adding missing ones and removing unreferenced ones.
Installation:
go install golang.org/x/tools/cmd/goimports@latest
Usage:
# Same as in the gofmt -> overwrite files with formatted version
goimports -w .
# List files whose formatting differs from goimport's
goimports -l .
What’s the difference from gofmt?
Will add the missing imports, f.e. if you used the fmt.Println but forgot to import the fmt package
Will remove the unused imports automatically
Example:
Before:
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("Keep Is Simple, Stupid!")
}
After:
package main
import "fmt"
func main() {
fmt.Println("Keep Is Simple, Stupid!")
}
The best tool is a one that you don’t have to remember about.
You can set up them in the IDE or CI, so they’re executed automatically.
My JetBrains GoLand’s code style is as follows:

For the VS Code it should look like this:
The settings.json file:
{
"go.formatTool": "goimports",
"editor.formatOnSave": true
}
For the GitHub’s CI / GitHub Actions:
- name: Install goimports
run: go install golang.org/x/tools/cmd/goimports@latest
- name: Verify formatting & imports
run: test -z "$(goimports -l .)"
If you have to choose one, use goimports. It's like gofmt on steroids, and it also saves you a few seconds per file.
Finally, Keep It Simple, Stupid!
gofmt: https://pkg.go.dev/cmd/gofmt
goimports: https://pkg.go.dev/golang.org/x/tools/cmd/goimports
Go tools: https://cs.opensource.google/go/x/tools
go fmt your code: https://go.dev/blog/gofmt
The Go Programming Language: https://cs.opensource.google/go/go