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

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 - The Classic That Keeps Things Tidy
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 - Formatting + Imports Management
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.Printlnbut forgot to import thefmtpackageWill 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!")
}
How to Use Them on a Daily Basis
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!
Sources
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




