IntelliJ for GoLang: A Comprehensive Guide

Author

Reads 1.1K

From above crop faceless male developer in black hoodie writing software code on netbook while working in light studio
Credit: pexels.com, From above crop faceless male developer in black hoodie writing software code on netbook while working in light studio

IntelliJ is a popular integrated development environment (IDE) that supports a wide range of programming languages, including GoLang.

GoLang support in IntelliJ is provided through the GoLang plugin, which offers features such as syntax highlighting, code completion, and debugging.

With the GoLang plugin, you can write, run, and debug your GoLang code directly within the IntelliJ interface.

IntelliJ's code completion feature for GoLang is particularly useful, as it suggests possible completions based on the context of your code.

Getting Started

To start using IntelliJ for Go, you'll need to install the Go plugin. The Go plugin is available in the JetBrains Marketplace, and it's free to download.

IntelliJ IDEA provides a Go runtime environment, which allows you to run and debug your Go programs directly from the IDE. This environment is based on the Go SDK.

You can create a new Go project in IntelliJ IDEA by selecting the Go project template. This template will set up the necessary project structure and configuration for your Go project.

The Go plugin also provides code completion and code inspection for Go. This means you'll get suggestions for function and variable names as you type, and the IDE will highlight potential errors in your code.

Additional reading: Golang Ide

Project Configuration

Credit: youtube.com, IntelliJ setup for Go (2018.09)

To create a Go project in IntelliJ IDEA, you can follow these simple steps: Select File | New | New Project, and then choose Go as the project language in the New Project dialog. Ensure that your Go installation location is specified in the GOROOT field, which IntelliJ IDEA usually detects automatically.

You can also add a new Go SDK version by clicking Add SDK and choosing one of the options. If you want to enable vendoring support, select or clear the Enable vendoring support automatically checkbox. Don't forget to specify any environment variables your project requires in the Environment field, such as GOPROXY.

Here's a quick rundown of the steps to create a new project:

  1. Select File | New | New Project.
  2. Choose Go as the project language.
  3. Specify your Go installation location in the GOROOT field.
  4. (Optional) Enable or disable vendoring support.
  5. (Optional) Specify environment variables.
  6. Click Create to create the project.

Once you've created your project, you can enable Go modules integration by opening settings, selecting Languages & Frameworks | Go | Go Modules, and then selecting the Enable Go modules integration checkbox.

Create A Project

To create a new project in IntelliJ IDEA, select File | New | New Project. Alternatively, navigate to New | Project on the Welcome to IntelliJ IDEA dialog.

Credit: youtube.com, 4 NEW PROJECT CONFIGURATION CREATING A PROJECT

You can also create a Go project by selecting New project from the list of available project types in the New Project dialog. Ensure that Go is selected as the project language in the Language list.

In the GOROOT field, specify the location of your Go installation. IntelliJ IDEA usually detects this location automatically, but you can click Add SDK () to choose a different Go SDK version.

You can enable or disable vendoring support automatically by selecting or clearing the Enable vendoring support automatically checkbox. This option is optional and can be ignored if you don't need it.

To specify any environment variables your project requires, such as GOPROXY, use the Environment field. However, be aware that this field is not suitable for setting tags for the GOEXPERIMENT variable.

Here are the steps to create a new project in a concise list:

  1. Select File | New | New Project or New | Project on the Welcome to IntelliJ IDEA dialog.
  2. Select New project from the list of available project types and choose Go as the project language.
  3. Specify the location of your Go installation in the GOROOT field.
  4. Enable or disable vendoring support automatically (optional).
  5. Specify any environment variables your project requires (optional).
  6. Click Create to create the project.

Change IML File Location

To change the IML file location for your Go projects in IntelliJ IDEA, you'll need to update the modules.xml file inside the .idea directory.

Photo Of Female Engineer Working On Her Workspace
Credit: pexels.com, Photo Of Female Engineer Working On Her Workspace

By default, the .iml file is created in the root directory of the project, but you can change this location to suit your needs. The modules.xml file is where you'll make the necessary updates.

Here are the steps to follow: In the Project tool window, open the .idea folder of the project.Update the fileurl and filepath attributes in the modules.xml file so that both point to the new IML file location inside .idea/.Move the IML file into the .idea directory.

This will ensure that your project is configured correctly and you can work efficiently in IntelliJ IDEA.

Enable Project Modules

To enable project modules, you need to select Languages & Frameworks | Go | Go Modules in the settings. This will open the Go Modules settings page.

Select the Enable Go modules integration checkbox to activate project modules. This is a crucial step to ensure that your project can use Go modules.

Close-up of an engineer using CAD software for vertical farm design on a desktop computer.
Credit: pexels.com, Close-up of an engineer using CAD software for vertical farm design on a desktop computer.

Click OK to save the changes. This will enable project modules for your current project.

You can also synchronize dependencies from the opened Go file by clicking a dependency in the import section, pressing Alt+Enter, and selecting Fix missing dependencies. This will update your project's dependencies to the latest versions.

Alternatively, you can synchronize dependencies from the go.mod file by double-clicking the go.mod file in the Project tool window, clicking a dependency declaration, and pressing Alt+Enter to select the action you want to perform.

If this caught your attention, see: Golang Dependency Management

Modules and Dependencies

In the Project tool window, Go modules are displayed with their full import path, making it easy to see all the dependencies in your project. This is especially helpful when you're working on a complex project with many dependencies.

The version of each Go Module is shown in a dimmed font color, which helps you distinguish between different versions of the same module. This is a great feature that saves you time and reduces errors.

Credit: youtube.com, Go Modules: Dependency Management in Go (Golang) for Beginners

To enable Go modules integration in your project, you can follow these simple steps: Press Ctrl+Alt+S to open settings, select Languages & Frameworks | Go | Go Modules, select the Enable Go modules integration checkbox, and click OK.

IntelliJ IDEA also provides a way to synchronize dependencies from the go.mod file. To do this, ensure that Go modules integration is enabled, double-click the MOD file in the Project tool window, click a dependency declaration, and press Alt+Enter to select the action you want to perform.

If you're working on a project with outdated dependencies, you can update them quickly using the inlay hint in the go.mod file. This feature highlights outdated dependencies and allows you to update them with just a click.

IntelliJ IDEA also includes inspections for deprecated and retracted versions of dependencies. These are marked with strikethrough text and indicate that the dependency is deprecated or has been retracted.

Here's a quick summary of how to update dependencies in go.mod:

  • Click the inlay hint to apply a quick-fix and update the dependency
  • Update all dependencies at once to the latest patch version or the latest available version
  • Limit updates to direct dependencies only if needed

You can also synchronize dependencies from a Go file by clicking a dependency in the import section, pressing Alt+Enter, and selecting Fix missing dependencies.

Code Management

Credit: youtube.com, Using IntelliJ IDE with Sync Gateway code base

Code management in IntelliJ for GoLang is a breeze, thanks to its robust features.

IntelliJ's code completion feature can suggest import statements for packages, which is a huge time-saver. This is especially useful when working with Go's module system, as seen in the section on "Go Modules".

With IntelliJ's project view, you can easily navigate and manage your Go project's dependencies. You can also use the "Go Modules" panel to view and edit your go.mod file.

Synchronize from File

To synchronize dependencies from a Go file, make sure Go modules integration is enabled. This is a crucial step to ensure that your project is set up correctly.

Click on a dependency in the import section of your Go file, and then press Alt+Enter to access a menu of options. One of the available actions is to select Fix missing dependencies.

Synchronizing dependencies from a file is a convenient way to manage your project's dependencies without having to navigate to the project tool window. This can save you time and effort, especially for larger projects.

To fix missing dependencies, simply click on the dependency in the import section and select Fix missing dependencies from the Alt+Enter menu. This will automatically update your project's dependencies.

Gfmt

Two women working together on code at a computer in a modern office setting.
Credit: pexels.com, Two women working together on code at a computer in a modern office setting.

Gfmt is a tool that formats Go source code, and IntelliJ IDEA includes a built-in import management system that handles imports on the fly.

To customize how imports are processed, you can open settings by pressing Ctrl+Alt+S and navigate to Languages & Frameworks | Go | Imports.

You can use gofmt to format code, and it's also possible to run both the built-in formatter and gofmt at once by enabling the On Reformat Code action option under Editor | Code Style | Go on the Other tab.

To format code in the currently opened file, navigate to Tools | Go Tools | Go Fmt File, or to format all code in the project, navigate to Tools | Go Tools | Go Fmt Project.

Here are the options for formatting code using gofmt:

  • Format code in the currently opened file: Tools | Go Tools | Go Fmt File
  • Format all code in the project: Tools | Go Tools | Go Fmt Project
  • Format code before committing changes to VCS: select the Go fmt checkbox in the Commit dialog

Environment and Tools

IntelliJ IDEA allows you to configure application execution parameters using environment variables.

You can store proxy server addresses for downloading dependencies using the GOPROXY variable. This can be useful if you're behind a proxy server.

For another approach, see: Golang Proxy

Credit: youtube.com, Some of GoLand IDE HotKeys on Parrot Security Linux

The GOPROXY variable specifies the proxy servers used to download dependencies when running the go command.

Here are some common environment variables you can use in IntelliJ IDEA:

  • GOPROXY: specifies the proxy servers used to download dependencies when running the go command.
  • GOSUMDB: specifies the checksum database used to verify that packages listed in the go.sum file are trusted.
  • GOPRIVATE: lists the modules considered private.
  • GONOPROXY: lists private modules that should not use proxy servers.
  • GONOSUMDB: lists private modules for which checksum validation is skipped.
  • Other: use this option to define custom variables.

IntelliJ IDEA also automatically detects Go module–related system environment variables and displays them in the Environment variables dialog.

Environment Variables

Environment variables are a powerful tool for configuring application execution parameters. They can be used to store proxy server addresses for downloading dependencies, define private packages, and set other relevant values.

You can use environment variables to specify the proxy servers used to download dependencies when running the go command. This is done by setting the GOPROXY variable.

IntelliJ IDEA provides templates for environment variables, including GOSUMDB, which specifies the checksum database used to verify that packages listed in the go.sum file are trusted.

The GOPRIVATE variable lists the modules considered private, and the go command skips proxy and checksum verification for these modules. This is a great way to keep sensitive information secure.

Additional reading: Golang Test Command

Credit: youtube.com, What are Environment variables? [10 of 20] | Bash for Beginners

Here are some common environment variables you can use:

  • GOPROXY: specifies the proxy servers used to download dependencies when running the go command.
  • GOSUMDB: specifies the checksum database used to verify that packages listed in the go.sum file are trusted.
  • GOPRIVATE: lists the modules considered private.
  • GONOPROXY: lists private modules that should not use proxy servers.
  • GONOSUMDB: lists private modules for which checksum validation is skipped.
  • Other: use this option to define custom variables, such as GOMODCACHE to change the default module cache location.

Installing SDK

Installing SDK is a straightforward process. Press Ctrl+Alt+S to open settings and then select Languages & Frameworks | Go | GOROOT.

To add a local copy of the Go SDK, click the Add SDK button and select Local. This will allow you to choose the SDK version from your hard drive.

Navigate to the file browser and find the SDK version that is on your hard drive. Click Open to complete the process.

Here's a quick summary of the steps:

  1. Press Ctrl+Alt+S to open settings
  2. Select Languages & Frameworks | Go | GOROOT
  3. Click the Add SDK button and select Local
  4. Navigate to the file browser and select the SDK version
  5. Click Open to complete the process

Installing Apps

Installing apps in your Go environment is a straightforward process that involves compiling the source file and placing the binary in the bin folder under GOPATH.

This allows you to simply call the app, making it easily accessible. Currently, there's no corresponding action to do this for you in IntelliJ IDEA unless you run it from the terminal tool window.

It's also possible to configure it as an external tool, but it's not necessarily required.

Debugging and Testing

Credit: youtube.com, Master IntelliJ Debugging: Essential Tips & Tricks for Efficient Debugging

Debugging and testing in IntelliJ for Go is a breeze thanks to the plugin's support for Delve, which eliminates the need to set up GDB and go through a tedious process.

With Delve, you can debug your app right out of the box, but keep in mind that you need version 1.5+ of Go for it to work.

Debugging with IntelliJ IDEA 15 is not fully compatible, so if you're missing the context menu to debug your app, that's why.

You can run your code using the context menu or hotkeys, such as Shift+Ctrl+R for Run and Shift+Ctrl+D for Debug.

On a similar theme: Vscode Golang Debugging

Vet

Vet is a powerful tool for identifying potential issues in your Go code. To run go vet, navigate to Tools | Go Tools | Go Vet File.

The output of go vet will appear in the Terminal tool window, where you can review the results.

For more insights, see: Golang Tools

Debugging

Debugging is a breeze with the latest plugin builds, which support debugging out of the box using Delve.

Credit: youtube.com, What is Testing and Debugging | Your go to 3 min Beginner's Guide | Ultimez Technology

You'll need version 1.5+ of Go for it to work, so make sure your Go version is up to date.

The plugin isn't fully compatible with IntelliJ IDEA 15, which is currently in EAP, and debugging doesn't work in this version.

If you're missing the context menu to debug your app, it's likely due to this incompatibility issue.

Build and Make Project options are available, but they have no effect in Go, so you'll need to use the context menu or hotkeys to run your code.

The hotkeys for running and debugging your code are Shift+Ctrl+R for Run and Shift+Ctrl+D for Debug.

Expand your knowledge: Golang Version Manager

Code Organization

Code Organization is a crucial aspect of any Go project. You can see a clear structure in the provided example, with folders like `src`, `test`, and `testdata` indicating a well-organized project.

The `.gitignore` file is also present, which is essential for ignoring unnecessary files in your Git repository. This ensures your commit history remains clean and focused on the actual code changes.

Credit: youtube.com, Learn Go Series - 3: Imports, Package Management and Project Structure in Go

The project's structure is further reflected in the file list, with clear separation between folders like `.idea` and `mock-sdks/go/default`. This organization makes it easier to navigate and work with the project's files.

This structure also includes a `README.md` file, which is a great place to provide project information and guidelines for contributors.

Formatting Code On The Fly

Formatting code on the fly is a game-changer for productivity. You can invoke it at any time using Alt+Cmd+L (on OSX 10.5+ keybinding).

IntelliJ IDEA provides code formatting, which is a huge time-saver. However, if you want to use GoFmt and have IntelliJ IDEA automatically format your code on save, you'll need to use File Watchers with GoImports.

Folders and Files

Code organization is crucial for any project, and one of the key aspects of it is how you structure your folders and files.

A good starting point is to have a clear hierarchy of folders, which can be seen in the example of the "mock-sdks/go/default" folder. This type of organization makes it easy to find and manage related files.

Credit: youtube.com, This Folder Structure Makes Me 100% More Productive

Having a dedicated folder for test files, such as the "test" folder, helps keep your project's codebase clean and organized. This is especially important for large projects with many dependencies.

Let's take a look at the actual folders and files in the example:

You can see that the "test" and "testdata" folders are separate, which is a good practice for keeping your project organized.

Tutorials

If you're looking to improve your code organization skills, let's start with some tutorials that can help you get started.

Philip Andrew's Intellij Go Plugin tutorial is a great resource for learning how to use plugins to enhance your coding experience.

To take your skills to the next level, I recommend checking out tutorials on specific topics, such as coding with Go or using Intellij plugins.

But first, let's take a look at some resources that can help you get started:

  • Philip Andrew's Intellij Go Plugin tutorial

Plugins and Extensions

The Google Go language plugin for IntelliJ Idea is an attempt to build an outstanding IDE for the Google Go language using IntelliJ IDEA.

Credit: youtube.com, Best Productivity Boosting IntelliJ IDEA Plugins

There's a plugin available that's specifically designed for Google Go, making it easier to work with this language within IntelliJ.

IntelliJ Idea can be used as a robust IDE for Google Go, offering a range of features and tools to enhance your development experience.

The Google Go language plugin for IntelliJ Idea is a great option for developers looking to streamline their workflow and improve their productivity when working with Google Go.

Frequently Asked Questions

Which IDE do you use for Golang?

We recommend using popular IDEs like VS Code, Sublime Text, or IntelliJ IDEA for a seamless Golang coding experience. These editors offer excellent language support and code highlighting.

Is IntelliJ GoLand free?

IntelliJ GoLand offers free licenses to students, faculty members, and open source contributors. For others, pricing options are available on the Subscription Options and Pricing page.

Does IntelliJ Ultimate include Golang?

IntelliJ IDEA Ultimate includes Go-specific coding assistance and tool integrations, making it a suitable choice for Golang development. Note that GoLand features are also available in IntelliJ IDEA Ultimate.

Judith Lang

Senior Assigning Editor

Judith Lang is a seasoned Assigning Editor with a passion for curating engaging content for readers. With a keen eye for detail, she has successfully managed a wide range of article categories, from technology and software to education and career development. Judith's expertise lies in assigning and editing articles that cater to the needs of modern professionals, providing them with valuable insights and knowledge to stay ahead in their fields.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.