What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Install goimports with go install golang.org/x/tools/cmd/goimports@latest. Run goimports -w file.go to format a Go file and update its imports in place, or use goimports -d file.go to inspect changes first.

What goimports does

goimports updates Go import declarations by adding imports needed by the code and removing imports that are no longer referenced. It also formats code in the same style as gofmt, so it can handle import maintenance and standard Go formatting in one step.

Install goimports

  1. Install the command with Go:

    go install golang.org/x/tools/cmd/goimports@latest
  2. Confirm that the Go installation’s binary directory is available on your PATH, then check that the command runs:

    goimports -h

The Go Tools project documents the go install command in its Go Tools guidance. The @latest suffix selects a moving latest version. At the time of the pkg.go.dev listing dated October 2, 2026, goimports was v0.51.0; use a specific version in the install command when you need a repeatable tool version, and record that version with your project or development setup. See the package listing for the current release information.

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

Run goimports on a file

Choose the command based on whether you want to preview, write, or locate formatting changes:

Command Result
goimports file.go Prints the formatted result, with updated imports, to standard output.
goimports -w file.go Writes the formatted result back to file.go.
goimports -d file.go Shows a diff without rewriting the file.
goimports -l path Lists files under the supplied path whose formatting differs.

Use -d before -w when you want to review what will change. The command also accepts input from standard input, which can be useful in editor or script integrations. These command behaviors and flags are documented in the goimports package documentation.

Control import grouping and formatting

Put local imports in a separate group

Pass -local with comma-separated import prefixes to place matching imports after third-party imports. For example, for a project whose module path begins with example.com/acme:

goimports -local example.com/acme -w main.go

Format without changing imports

Use -format-only when you want goimports to format code but leave import declarations alone:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
goimports -format-only -w main.go

Run goimports automatically from an editor

The goimports documentation describes using it instead of gofmt in editor save hooks. Configure your editor or Go plugin to invoke goimports when saving Go files; the exact setting varies by editor and can change between plugin versions. Check the current instructions for your editor rather than copying an old configuration example. The package documentation includes editor examples and the rationale for using goimports as a gofmt-on-save replacement.

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

Exclude directories from GOPATH import scanning

When scanning under GOPATH, goimports respects $GOPATH/src/.goimportsignore. Put directory names relative to that configuration file on separate lines to exclude them. Blank lines and lines beginning with # are allowed; glob patterns and regular expressions are not supported. Use the -v flag to inspect what goimports is doing while diagnosing unwanted scanning or import suggestions. Refer to the package documentation for the configuration details.

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.