goimports formats Go source code and keeps its import list in step with package usage. It wraps gofmt, adds missing imports, and removes unused ones that could cause compiler errors after a refactor. For routine formatting, it can take the place of go fmt. section 6.1.1 section 1.2.4
To make it part of a workflow:
- Install it with `go install golang.org/x/tools/cmd/goimports@latest`, then run `goimports [FILE_PATTERN].go` on the files you want to format. section 6.1.1
- In GoLand, add goimports as a File Watcher for saved `.go` files. In VS Code, enable Format on Save. section 6.1.1
After editing imports
Suppose a Go file still imports a package removed during a refactor, or uses a package without importing it. Run goimports to remove the unused import or add the missing one while formatting the file. section 6.1.1
