VS Code Go Extension: Don't Open Imports in the Browser

In VS Code, clicking on a Go import will open the GoDoc of the imported package in the browser. This behavior is driven by gopls and can be disabled.

July 26, 2021 in VS Code

When clicking on an imported Go package in VS Code, its GoDoc at pkg.go.dev will be opened by default. This is a feature of the Go Language Server (gopls). It works fine for packages from the standard library, but will display an error on the website if it is a private package. In most cases, when clicking onto an imported package, I want to navigate into that package.

Don’t Open the Docs in the Browser

All gopls settings can be configured in the global settings.json file in VS Code. The abovementioned behavior is configurable via the importShortcut key, which is an enum with three possible values:

  • Link: Open the link in the Go documentation.
  • Definition: Go to the package definition. This is probably what you want.
  • Both": Combines Link and Definition.

Therefore, in order to only navigate to the package definition, your gopls configuration has to look like this:

{
    ...
    "gopls": {
        "importShortcut": "Definition"
    }
}

For further navigation settings of gopls, take a look at the official reference.

💫graph: A library for creating generic graph data structures and modifying, analyzing, and visualizing them.
💫
graph: A library for creating generic graph data structures and modifying, analyzing, and visualizing them.