IntelliJ IDEA 2024.3 Help

GOROOT and GOPATH

Go tools expect a certain layout of the source code. GOROOT and GOPATH are environment variables that define this layout.

GOROOT is a variable that defines where your Go SDK is located. You do not need to change this variable, unless you plan to use different Go versions.

In Go development, the GOPATH variable, which defaults to $HOME/go on Unix and %USERPROFILE%\go on Windows, serves several purposes:

  • Binaries are installed to $GOBIN by go install, with $GOBIN defaulting to $GOPATH/bin.

  • Modules are cached in $GOMODCACHE by go get, defaulting to $GOPATH/pkg/mod.

  • Checksum database state is cached in $GOPATH/pkg/sumdb by go get.

For details about the GOPATH variable, refer to the go command documentation.

The concept of GOPATH development mode, which precedes Go modules, is now deprecated. Prior to Go modules, the GOPATH was used to install binaries and compiled packages, as well as downloading source code to specified paths. For more information about developing with GOPATH, refer to GOPATH development mode at go.dev.

Consider the following video where we change the Go SDK from 1.16 Beta 1 to 1.20.1.

GOROOT

Configure GOROOT

Ensure that the provided path to the folder with Go SDK includes bin and src folders.

  1. Open settings (Ctrl+Alt+S) and navigate to Languages & Frameworks | Go | GOROOT.

  2. Click the Add SDK button the Add SDK button and select Local.

  3. In the file browser, navigate to the SDK version that is on your hard drive.

  4. Click Open.

    Select a local copy of Go SDK

Download the Go SDK

  1. Open settings (Ctrl+Alt+S) and navigate to Languages & Frameworks | Go | GOROOT.

  2. Click the Add SDK button (the Add SDK icon) and select Download.

  3. From the Version list, select the SDK version.

  4. In the Location field, specify the path for the SDK. To use a file browser, click the Browse icon the Browse icon.

  5. Click OK to close the Download Go SDK dialog.

    As you click Apply or OK on the GOROOT page, IntelliJ IDEA will start downloading and unpacking the Go SDK.

    Download the Go SDK

Using asdf

asdf is a tool that manages multiple versions of different programming languages and utilities. It allows switching between different Go versions for different projects. Also, asdf can set different scopes: asdf global, which sets a default version for the entire system, and asdf local, which sets a version for a specific directory.

IntelliJ IDEA supports asdf local and asdf global commands, including specifying multiple versions (for example, asdf local golang 1.21.0 1.20.8).

Select an asdf Go version for the project

  1. Open settings by pressing Ctrl+Alt+S and navigate to Go | GOROOT. .

  2. From the list, select the Go version that you need.

GOPATH

Configuring GOPATH for different scopes

You can configure GOPATH for the following scopes:

  • Global GOPATH: settings apply to all projects of a specific installation of IntelliJ IDEA.

  • Project GOPATH: settings apply only to the current project.

  • Module GOPATH: settings apply only to one module. A module can have an SDK that is different from those configured for a project. They can also carry a specific technology or a framework.

Types of settings

If you specified all three scopes, IntelliJ IDEA selects the narrowest scope first.

  1. Open settings (Ctrl+Alt+S) and navigate to Languages & Frameworks | Go | GOPATH.

  2. Depending on the scope that you want to apply, select the corresponding section (Global GOPATH, Project GOPATH, or Module GOPATH) and click the Add button the Add button.

  3. In the file browser, navigate to the directory that you want to associate with GOPATH.

    In the following example, we configured to use different GOPATH directories for different scopes. IntelliJ IDEA will use the Module GOPATH as it is the narrowest scope configured.

    Configuring GOPATH for different scopes
Last modified: 12 November 2024