Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use 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.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.