The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →goimports updates Go import declarations by adding missing imports and removing unreferenced ones, then formats the source in the same style as gofmt. It is useful when you want import maintenance as part of formatting—whether you run it from a shell or configure your editor to apply it on save. It does not establish that a program compiles or behaves correctly.
What goimports does
The Go project’s goimports command documentation says it “updates your Go import lines, adding missing ones and removing unreferenced ones.” It also formats code in the gofmt style, so the documentation describes it as a possible replacement for an editor’s gofmt-on-save hook.
As an Amazon Associate I earn from qualifying purchases.
The distinction is straightforward: gofmt formats Go source, while goimports also maintains the file’s imports. The Go Tools repository describes the tool in the same terms: formatting like go fmt plus inserting imports for packages a file requires. Import cleanup and formatting are not a substitute for compiling, testing, or reviewing a change.
Install goimports
Install the command with Go’s go install command:
#1 Best Overall
go install golang.org/x/tools/cmd/goimports@latest
This requests the latest available version of the package at installation time. The package page is the place to check its current release information; the version can change, so a fixed version should not be inferred from this command.
Run it and control changes
From a shell, pass Go files or directories as arguments. The command documentation describes these flags for reviewing or applying formatting:
| Option | Effect | When to use it |
|---|---|---|
-d |
Displays a diff instead of writing the changes. | Preview import and formatting edits before applying them. |
-w |
Writes the result to the source files. | Apply the edits after deciding they are appropriate. |
-l |
Lists files whose formatting differs. | Find files that would be changed. |
-format-only |
Formats without fixing imports. | Use when you want formatting but do not want import updates. |
These options are documented in the goimports command source. For a cautious first pass, use -d to inspect a diff; use -w only when you intend to update the files.
Choose the source directory context
The -srcdir flag sets the source directory context. When working with a single file, its value can instead be the complete filename. Consult the command’s current help or source if your invocation depends on this behavior.
Group project imports with -local
The -local option accepts comma-separated prefixes and moves imports whose paths begin with those strings after third-party packages. For example:
goimports -local example.com/acme,git.example.net/team -d ./...
This is a string-prefix grouping rule: it controls import order and does not verify that a path is genuinely part of your organization or repository. Review the resulting diff to confirm the grouping fits your project’s conventions.
Rank #4
Use goimports in an editor
Because it formats in the gofmt style while maintaining imports, goimports can be configured as an editor’s formatter or save action. The command documentation includes examples for Emacs and Vim, but the exact setup depends on your editor and its current integration. Configure the editor’s supported formatter or save action to invoke goimports, then check a changed Go file to confirm that imports and formatting are handled as expected. See the command documentation for its editor examples.
Recommended Free Tools
The documentation also describes a scan-exclusion file at $GOPATH/src/.goimportsignore. Its entries may be blank lines, comments beginning with #, or directory names relative to that file; glob patterns and regular expressions are not allowed. The documented mechanism is tied to that GOPATH path, so do not assume it applies universally to every editor or workspace setup. The documentation recommends -v when checking behavior.
Best Value
When to choose goimports
- Choose
goimportswhen formatting should also add missing imports and remove unused ones. - Use
-format-onlywhen you want formatting but not import repair. - Use
-dfor a reviewable preview and-wto write changes. - Use
-localwhen your project wants selected import prefixes grouped after third-party imports.
The Go Tools repository also describes gopls as an LSP server that supports IDE-like editor features. That is a separate repository description, not evidence that gopls is a universal replacement for running the standalone goimports command; choose an editor workflow based on the integration it supports.
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.




