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

First identify what is changing the file: the standalone goimports command or your editor’s gopls organize-imports action. Both remove imports they consider unreferenced. If the package is needed for initialization side effects, use a blank import; if its only use is temporarily commented out, disable automatic organization on save or run it manually until the code is restored.

Why an import disappears

The Go project’s goimports documentation says the command updates import lines by adding missing imports and removing unreferenced ones. It also formats code in the style of gofmt. An editor may instead—or also—run gopls’s source.organizeImports action when you save. The gopls transformation documentation says this action removes duplicate or unused imports, adds imports for undefined symbols, and sorts imports.

A common debugging case is that the import’s only use is commented out. The gopls documentation’s explanation gives this as a reason some developers prefer not to organize imports automatically.

Identify what is organizing imports

  1. Save a copy of the file, then note whether the import changes only when saving, when running a formatter, or after invoking an editor action.
  2. Check the editor’s Go formatter and save-time code actions. Look for standalone goimports, gopls organize imports, or both.
  3. Run the configured tool directly against the file, if appropriate, to see whether it reproduces the change. For standalone goimports, the documentation describes the -v flag for inspecting behavior.
  4. Check which version the editor invokes. The package page reported goimports v0.51.0, published October 2, 2026; your editor may use a different installed version. The documented go install golang.org/x/tools/cmd/goimports@latest command installs the latest version, not a reproducible version pin.

Choose the fix that matches the import’s purpose

The file refers to a package symbol

Make sure the saved file contains a live reference to the package’s identifier. Check the spelling, any import alias, the package name, and the import path. A reference elsewhere in the package does not necessarily make this file’s import useful. If a tool inserts a different path, verify that the chosen package exports the name you need and inspect your module or workspace context.

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.

A 2017 Go issue report describes neighboring files influencing package selection in a particular GOPATH layout. It is a historical, environment-specific report—not evidence of a general current defect. Use it as a prompt to inspect context and tool versions, not as a diagnosis by itself.

The package is imported for initialization side effects

Use a blank import to make that purpose explicit:

import _ "example.com/driver"

This is appropriate when the package’s initialization side effect is the reason for importing it, rather than when you intend to refer to one of its names.

The only use is temporarily commented out

Either leave the reference active while debugging or stop automatic import organization until you restore the code. You can organize imports manually afterward.

You want to control organization manually in VS Code

The gopls documentation shows disabling the organize-imports save action for Go files with this setting:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
{
  "[go]": {
    "editor.codeActionsOnSave": {
      "source.organizeImports": false
    }
  }
}

Then invoke the editor’s Organize Imports action when you choose. Other editor clients have different settings: locate the configured save hook or code action instead of copying this VS Code setting blindly.

A standalone goimports hook is running

Change the formatter or save hook that invokes goimports; this is separate from disabling gopls’s organize-imports action. The goimports package documentation describes using the command in a gofmt-on-save hook. Its .goimportsignore setting controls directories scanned under GOPATH for import discovery; it is not a general option for retaining unused imports.

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

If you mean a tool dependency, manage it separately

An import in ordinary compiled source code is not a general mechanism for keeping a developer tool in a module’s dependency graph. For Go 1.24 and later, the Go documentation describes go get -tool ... for tool dependencies. For earlier Go versions, it documents an alternative using a blank import in a Go file excluded from the build with build constraints, then running the tool with go run and its full package path. See Managing dependencies: Tools. That approach is for tool dependency management, not for preserving an ordinary unused import in compiled source.

Use this quick decision check

  • Named symbol: Confirm the current file has a live reference and the import path and alias are correct.
  • Initialization side effect: Use a blank import.
  • Temporary debugging: Keep the reference active or disable the save-time organize action until the code is restored.
  • Manual control: Disable the relevant save hook and organize imports on demand; distinguish the editor’s gopls action from a standalone formatter.
  • Tool dependency: Use Go’s tool-dependency mechanism appropriate to your Go version, rather than an ordinary source import.

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.

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