Features

Go Modules Support Improvements in GoLand 2020.1

GoLand 2020.1 brings a lot of new features to Go Modules support, and today we’ll take a look at them and learn how to use them to maximize our productivity.

We’ll cover:

Before we continue, you might be interested in our series of blog posts on Go Modules support, which describes how to get started, manage dependencies, visualize dependencies, and use versioning.

Go Environment Variables

Until 2020.1, you could only configure the GOPROXY environment variable.

Starting with 2020.1, in addition to GOPROXY, you can also configure other variables like GOSUMDB, GOPRIVATE, GONOPROXY, and so on. If a variable is not on the list, you can choose the Other option from the list of variables and define your own.

There are two ways to configure these variables: by using the Environment field when you create a new Go Modules-based project, or with Settings/Preferences | Go | Go Modules for existing projects.

Configure Go Environment Variables in Go Modules

Note: The environment variables are also passed to the go build and your application and tests.

Improved Vendoring support

Go 1.14 has changed how vendoring works, and GoLand 2020.1 helps you keep up to date with the changes. Before Go 1.14, you had to enable the vendoring mode manually. Now it will automatically be enabled whenever a vendor folder exists.

Vendoring support improvements

Completion in go.mod for require statements

Completion for Go Modules’ import paths is now available in require statements. Only modules present in the Go Modules cache will be used. Versions currently have to be added manually.

Module import path completion in require directive

Completion in go.mod for replace statements

Similar to the require statement, the replace statement also supports code completion. This works for both the name and the local path of the module.

Completion in require directives of go.mod files

Detect local paths in replace statements when committing

When using the replace statement to work on a dependency locally, it’s easy to commit that replacement to the repository, which can cause trouble later.

If you commit from the IDE, it will now run an inspection for replaced statements and warn you about it, so that you can always be sure that this problem won’t happen.

Detect local paths in replace directives when committing

Note: Ensure that the Perform code analysis feature is enabled in the commit options to use this feature.

Rename and Move refactoring are aware replace statements

If the project needs to replace a module with a custom version, we can use the replace statement to point to that module. When times comes to perform maintenance, such as tidying up the locations, we can make use of the Rename and Move refactorings. Now, the IDE will automatically correct the locations of the modules in replace statements, so that everything is up to date and the code can work as before.

Rename and Move Refactoring are aware replace statements

Find Usages of a folder will show applicable replace statements

In line with the rest of the improved support for the replace statement, it is possible to see whether a folder is being used as part of a replace statement. To use this feature, select a folder in the Project View tool window and invoke Find Usages via Alt+F7 on Windows/Linux or ⌥+F7 on macOS.

Find Usages of folder in replace statements

Cleanup go.mod

Over the course of a feature’s development cycle, it will be necessary to add imports, remove them, and so on. So, before we can commit the go.mod file, we can clean it up using the Sync dependencies of the <module> quick-fix.

Cleanup go.mod file

That’s it for today. We’ve discussed how to effectively use the updated Go Modules support to code cleaner and faster using GoLand. Let us know your thoughts in the comment section below, on our issue tracker, or tweet us @GoLandIDE.

image description

Discover more