goimports updates Go import declarations and formats source code in gofmt style. It adds imports for references that need them, removes imports the file no longer uses, and chooses additions by looking for available packages that export the needed symbols. That makes it a practical import-maintenance tool, not a guarantee that it can infer intent when several packages could fit.
What goimports does
goimports is a command in the Go tools module, golang.org/x/tools. It combines formatting with import maintenance: it adds missing imports, removes unreferenced imports, and formats Go code in the same style as gofmt. The official command documentation describes it as updating import lines by “adding missing ones and removing unreferenced ones.”
As an Amazon Associate I earn from qualifying purchases.
For example, after you write code that refers to a package-qualified name without importing its package, goimports can look for a package that supplies the referenced symbol and add a candidate import. If you later remove the code that used an import, the tool can remove that now-unreferenced import.
Recommended Free Tools
How goimports finds a package for a missing import
The internal imports API models references in a source file as package names associated with the symbols used from those packages. It then asks a data source to resolve missing references against packages that can provide those symbols. The relevant match is not just “a package with a familiar name”: a candidate needs to export the required symbol or symbols.
#1 Best Overall
In practice, goimports searches package information available to it and uses the file’s context to choose among candidates. Its API describes how references are resolved; it does not promise that the tool can determine the programmer’s intent when multiple candidates could satisfy a reference.
Why project context matters
The filename supplied for processing matters because its directory can affect which imports are selected. The resolver also depends on the build system or environment used to find packages. For that reason, running goimports with the correct file path and project environment can affect the available candidates. The public API documents this directory influence and the resolver contract in the imports package documentation.
Why it might add a package you did not expect
If more than one available package exports a symbol your file references, the tool may select a candidate based on the information and context it has. An added import is therefore a candidate resolution, not proof that the package matches the author’s intended design. Review unexpected changes, especially where names are common or more than one package could plausibly provide them.
Does goimports remove unused imports?
Yes. Its documented behavior includes removing imports that are no longer referenced. It also inserts missing imports, so it can make both kinds of import changes during the same formatting pass.
Is goimports the same as gofmt?
No. Both produce Go formatting in the gofmt style, but goimports also maintains import declarations. Use gofmt when you want formatting without import discovery; use goimports when you want imports added and removed as part of that workflow. The Go tools module describes goimports as inserting imports required by a file after editing in addition to its formatting role (x/tools module overview).
If you use the imports API directly rather than the command, its FormatOnly option disables import insertion and deletion. That lets an API caller request formatting without import maintenance; see the API documentation.
Rank #4
Install goimports
The command documentation consulted for this article reports x/tools v0.51.0, published October 2, 2026, and shows this install command:
go install golang.org/x/tools/cmd/goimports@latest
The @latest suffix follows the latest available module release, so it does not pin a fixed version. If you need repeatable tool versions across machines or builds, replace latest with the specific version you want to use.
Best Value
Configuration and troubleshooting
Inspect what the command is doing
The command’s -v flag can provide information about its activity. Use it when investigating an unexpected import choice.
Limit scanning with .goimportsignore
The command documentation describes $GOPATH/src/.goimportsignore for excluding directories under GOPATH from scans for Go files. The file accepts blank lines, comments that begin with #, and directory names relative to the configuration file. It does not support glob or regular-expression patterns. This documented setting is scoped to GOPATH directories; it should not be treated as a universal ignore file for every Go module or editor setup. Details are in the command documentation.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →




