GoLand 2020.3 Help

Go modules

The go get command downloads the latest version of dependencies from the master branch of the Go repository. In some cases, this approach does not work. For example, when you have two projects that depend on different versions of a third-party library. With Go modules (formerly known as vgo), you can download and import the necessary library versions per project. Read more about Go modules at blog.golang.org.

Create a project with integration of Go modules

  1. Select File | New | Project.

  2. In the New Project page, select Go modules.

  3. In the Location field, specify the path where you want to store your project.

  4. In the GOROOT field, specify the location of your Go installation. Usually, the location is defined automatically.

    To change or install a new version of Go SDK, click the Add SDK button and select Local to choose the Go SDK version on your hard drive, or select Download to download Go SDK from the official repository. For more information about installing Go SDK, see Installing Go SDK.

  5. From the GOROOT list, select the Go version that you want to use with Go modules.

  6. In the Environment field, specify all the necessary environment variables. For example, the GOPROXY environment variable. Read more about environment variables in the Environment variables section.

  7. (Optional) To use vendoring mode, select the Vendoring mode checkbox. For more information about vendoring, see Vendoring.

  8. Click Create.

    Integration with Go modules

Working with Go modules

In the Project tool window (View | Tool Windows | Project), Go modules are displayed with their full import path. The version of each Go Module is shown in a muted font color to help you distinguish between them.

Enable Go modules in a project

A new Go modules project already has Go modules enabled. If you pulled your Go modules project from Github, you need to enable Go modules manually.

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

  2. Select the Enable Go modules integration checkbox.

  3. Click OK.

    Enable Go modules in a project

Synchronize dependencies from the opened Go file

  1. Ensure that Go modules integration is enabled. For more information about enabling Go modules integration, see Enable Go modules in a project.

  2. Click a dependency in the import section, press Alt+Enter and select Sync dependencies of <project_name>.

    Fetch dependencies

Synchronize dependencies from go.mod

You can fetch missed dependencies and remove unused dependencies.

  1. Ensure that Go modules integration is enabled. For more information about enabling Go modules integration, see Enable Go modules in a project.

  2. In the Project tool window (View | Tool Windows | Project), double-click the MOD file.

  3. Click a dependency declaration.

  4. Press Alt+Enter and select Sync dependencies of <project_name>.

    Fetch a source with Go modules

Create a diagram of dependencies

The go.mod file lists dependencies for your project. You can use this file to build a diagram of dependencies.

Before you begin, ensure that your project is under GOROOT.

  1. Enable Go modules in your project.

  2. Right-click the go.mod file in your project and select Diagrams | Show Diagram.

    Create a diagram of dependencies

Enable vendoring mode

You can toggle vendoring mode only in Go versions 1.13 and earlier. In Go 1.14 RC, automatic vendoring mode became a built-in Go feature.

  1. Open settings by pressing Ctrl+Alt+S of the Go modules project, navigate to Go | Go modules.

  2. Select the Enable vendoring support automatically checkbox and click OK.

    Automatic vendoring mode in Go 1.13 and earlier versions
  1. In the Project tool window (View | Tool Windows | Project), double-click the go.mod file.

  2. Click a dependency import path and click Navigate | Declaration or Usages Ctrl+B.

    Navigate from a dependency import path to package source files

Environment variables

Environment variables provide a way to set application execution parameters. Environment variables can store addresses of proxy servers that you want to use to download dependencies (GOPROXY), names of packages that are considered private (GOPRIVATE), and other values. In GoLand, you can use the following templates for environment variables:

  • GOPROXY: defines proxy servers that must be used to download dependencies. These proxy servers are used when you trigger the go command. Read more about GOPROXY in Module downloading and verification at golang.org.

  • GOSUMDB: identifies the name of the checksum database. The checksum database verifies that your packages from the go.sum file are trusted. Read more about GOSUMDB in Module authentication failures at golang.org.

  • GOPRIVATE: lists packages that are considered private. The go command does not use the GOPRIVATE or checksum database when downloading and validating these packages. Read more about GOPRIVATE in Module configuration for non-public modules at golang.org.

  • GONOPROXY: lists packages that are considered private. The go command does not use the proxy when downloading these packages. GONOPROXY overrides GOPRIVATE.

  • GONOSUMDB: lists packages that are considered private. The go command does not use the checksum database during validation of these packages. Overrides GOPRIVATE.

  • Other: any environment variable that you want to introduce. For example, you can introduce the GOMODCACHE environment variable that changes the default location of the Go modules cache from $GOPATH/pkg/mod to a user-defined location (for example, $WORK/modcache).

Introduce an environment variable in a project

  1. When you create a new project, click Go Modules in the New Project window.

    For existing projects, open settings by pressing Ctrl+Alt+S and navigate to Go | Go modules.

  2. In the Environment field, click the Browse icon at the end of the field.

  3. In the Environment variables window, click the Add button (the Add button) and select the template that you want to add.

    Introduce an environment variable in a new project

Get information about environment variable template

  • In the Environment variables window, click the Explain variable button (the Explain variable button).

    Introduce an environment variable in a new project

Installing Go SDK

Select a local copy of the Go SDK

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

  1. In the New Project dialog, select Go modules.

  2. Click 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. In the New Project dialog, select Go modules.

  2. Click the Add SDK button 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.

    Download the Go SDK

Productivity tips

Notify about replacements of local paths in go.mod file

Before you commit your changes, GoLand can show you a notification that you replaced local paths in the go.mod file. This might help you to avoid situations when these replacements are committed by mistake.

  1. Press Ctrl+K.

  2. In the Commit tool window Alt+0, click the Show Commit Options icon (the Show Commit Options icon) and select the Perform code analysis checkbox.

    Notifications about replacements of local paths in the go.mod file
Last modified: 18 October 2020