IntelliJ IDEA 2022.2 Help

Go

By default, IntelliJ IDEA suggests creating a Go modules project. With Go modules, you do not need to keep your project files under GOPATH and can easily manage dependencies in your project. Read more about Go modules at go.dev.

Create a project with Go modules integration

  1. Select File | New | New Project.

    Alternatively, click New Project in the Welcome to IntelliJ IDEA dialog.

  2. In the Name field, type a name of your project.

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

  4. If you want to initiate a git repository for the project, select the Create Git repository checkbox.

  5. In the Language list, click the Install Plugin icon (the Install Plugin button), and select Go. If you have already installed the Go plugin, select it from the Language list.

  6. If the Go plugin is not installed, in the Plugins dialog, find the Go plugin and click Install.

  7. Click OK to close the Plugins dialog.

  8. In the New Project dialog, select New project from the list of available projects.

    Ensure that Go is selected as a project language in the Language list.

  9. 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.

  10. (Optional) Select or clear the Enable vendoring support automatically checkbox.

  11. (Optional) In the Environment field, specify environment variables that you need for your project. For example, the GOPROXY environment variable. Read more about environment variables in the Environment variables section.

  12. Click Create.

    Download Go SDK

Change the IML file location for Go projects

When you create a Go project in IntelliJ IDEA, you can specify a directory for the .iml file. The default location is the root directory of the project.

If you want to change the IML file location for existing Go projects in IntelliJ IDEA, you need to modify the modules.xml file in the .idea directory.

Change the IML file location for existing Go projects

  1. In the Project tool window, navigate to the .idea folder of the project.

  2. Add .idea/ to fileurl and filepath attributes of the IML file location.

  3. Move the IML file to the .idea directory.

    Change the IML file location for existing Go projects

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 dimmed font color to help you distinguish between them.

Go Module is shown in a dimmed font color

If you create a new Go modules project in the IDE, Go modules are already enabled. If you pulled your Go modules project from Github, you need to enable Go modules manually.

Enable Go modules in a project

  1. Press Ctrl+Alt+S to open the IDE settings and select Languages & Frameworks | Go | Go Modules.

  2. Select the Enable Go modules integration checkbox.

  3. Click OK.

    Enable New project in a project

Working with dependencies

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 New project in a project.

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

    Fetch dependencies

You can fetch missed dependencies and remove unused dependencies.

Synchronize dependencies from go.mod

  1. Ensure that Go modules integration is enabled. For more information about enabling Go modules integration, see Enable New project 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 action that you want to perform. You can select between the following options:

    • Sync dependencies: fetches and downloads missed dependencies and removes unused ones by calling go mod tidy/vendor. In replace directives, the IDE would not download or delete missed dependencies, and the lines with these unused dependencies would be marked red. It is not an error and does not affect how the application works.

    • Download all modules to the module cache: fetches and downloads all the dependencies and places them under the External Libraries in the Project tool window (View | Tool Windows | Project).

    • Download <module_name> to the module cache: fetch and download the selected module and place it under the External Libraries in the Project tool window (View | Tool Windows | Project).

    Download all modules to the module cache

Configure automatic run of 'go list' for 'go.mod'

  1. Open settings by pressing Ctrl+Alt+S and navigate to Build, Execution, Deployment | Build Tools

  2. Select or clear the Reload project after changes in the build scripts option.

  3. You can set the following options depending on your workflow:

    • Any changes: run go list after any modification of go.mod. By default, IntelliJ IDEA would automatically run go list commands after every modification of go.mod.

    • External changes: do not run go list automatically when you edit the file inside the IDE. After you finished editing files in the IDE, click the Load Go modules Changes icon (the Load Go modules Changes icon) to apply and load your changes.

      Load Go modules Changes icon
    • If you do not want to run go list after every modification of go.mod, clear the Reload project after changes in the build scripts checkbox.

      If the checkbox is cleared, you will see the Load Go modules Changes icon on all types of changes: external and internal.

    Configuration for running 'go list'

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.

  1. Enable New project in your project.

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

    Create a diagram of dependencies

Toggle vendoring mode

You can toggle vendoring mode 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 and navigate to Languages & Frameworks | Go | GOROOT.

  2. From the GOROOT list, select the Go version 1.13 or earlier.

  3. Click the New project menu item.

  4. Clear or 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. Right-click a dependency import path and click Go To | 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 IntelliJ IDEA, 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. Press Ctrl+Alt+S to open the IDE settings and select Languages & Frameworks | 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

  1. Press Ctrl+Alt+S to open the IDE settings and select 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.

    Installing Go SDK

Download the Go SDK

  1. Press Ctrl+Alt+S to open the IDE settings and select Languages & Frameworks | Go | GOROOT.

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

    Installing Go SDK

Productivity tips

Notify about replacements of local paths in go.mod file

Before you commit your changes, IntelliJ IDEA 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 or select Git | Commit from the main menu.

  2. In the Commit tool window Alt+0, click the Show Commit Options icon (the Show Commit Options icon) and select the Analyze code checkbox. For more information about committing options, see Commit and push changes to Git repository.

Last modified: 29 November 2022