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.
- Go Environment Variables
- Improved Vendoring support
- Completion in go.mod for require statements
- Completion in go.mod for replace statements
- Detect local paths in replace statements when committing
- Rename and Move refactoring aware replace statements
- Find Usages of a folder will show applicable replace statements
- Cleanup go.mod
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.
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.
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.
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.
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.
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.
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.
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.
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.