DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

goimports: Fix and Format Go Imports

goimports formats Go code in gofmt style while adding missing imports and removing unreferenced ones. Learn how to install it and control its changes.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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

Install goimports

Install the command with Go’s go install command:

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.

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

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.

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.

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

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.

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

When to choose goimports

  • Choose goimports when formatting should also add missing imports and remove unused ones.
  • Use -format-only when you want formatting but not import repair.
  • Use -d for a reviewable preview and -w to write changes.
  • Use -local when 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.