iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Install goimports with go install golang.org/x/tools/cmd/goimports@latest. To put your project’s imports after third-party imports, run it with -local and a prefix matching your Go import path, such as goimports -local=example.com/acme ./path/to/file.go. For editor automation, choose either the standalone goimports command or your editor’s gopls import-organizing action, and avoid overlapping save actions.
Install and run goimports
goimports is part of the Go tools module. Install the command with:
go install golang.org/x/tools/cmd/goimports@latest
Then run it on a file to update its imports and format the source in gofmt style:
goimports ./path/to/file.go
By default, the command adds missing imports and removes imports it considers unreferenced, as well as formatting the file. See the goimports package documentation.
#1 Best Overall
Group project imports after third-party imports
Pass -local one or more comma-separated import-path prefixes. Imports whose paths begin with a listed prefix are placed after third-party imports:
goimports -local=example.com/acme ./path/to/file.go
For a project with more than one local prefix, separate them with commas:
goimports -local=example.com/acme,example.net/shared ./path/to/file.go
Use module or import-path prefixes, not a package identifier that does not match the import paths in your code. The command flag is documented in the goimports source; Go’s package documentation describes the import-update behavior.
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 →Choose whether goimports should edit imports
Use the default when imports should be maintained
The normal mode both formats code and fixes imports: it can add missing imports and remove unused ones. This is useful when running the command manually or from an editor save hook that should keep imports in sync with the file.
Use format-only mode when imports must remain untouched
Add -format-only to avoid import fixes while formatting the file. The command describes this mode as effectively gofmt with import sections grouped:
goimports -format-only ./path/to/file.go
This flag is documented in the goimports command source.
Rank #4
Configure an editor to organize imports on save
Use standalone goimports
Configure the editor’s formatter or before-save action to invoke goimports, including any -local prefixes your project needs. The official package documentation includes an Emacs example using gofmt-command and gofmt-before-save; see the package documentation.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsUse gopls in VS Code
The VS Code Go extension documents gopls as its default formatter. If you specifically select a tool such as goimports, the extension runs that tool instead. Check the active formatter and save actions in your setup so you know which integration is making import edits. The extension’s settings are documented at vscode-go settings.
Best Value
Use gopls organize-imports
Alternatively, let gopls organize imports through its source.organizeImports code action. Configure its local setting with comma-separated local import prefixes to control grouping. This is distinct from running the standalone goimports binary; choose one import-editing route for a given save action rather than assuming both are active. See the gopls transformation documentation.
Understand import selection and scan exclusions
When adding an import, gopls uses workspace and module-cache information as part of its package-selection heuristics. The official documentation notes that the result can sometimes be surprising, so review a newly added import if it does not look like the intended package. These heuristics are described in the gopls transformation documentation.
The $GOPATH/src/.goimportsignore file is documented as a way to exclude directories from goimports scans in GOPATH. It accepts blank lines, comments beginning with #, and directory names relative to the ignore file; it does not support globs or regular expressions. The documentation describes this as GOPATH scan control, not a universal module-mode ignore setting. See the goimports package documentation.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →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.

