When using IntelliJ IDEA with the Go plugin, or GoLand and using Go 1.11 or newer, you can enable Go modules by going to Preferences -> Languages & Frameworks -> Go -> Go Modules (vgo) and check the box 'Enable Go Modules (vgo) integration'.
When you have a project which was properly initialised by go mod init
, and therefore has a go.mod
and go.sum
file, it will download the dependencies when building from command line with go build
or from IntelliJ by pressing the green play icon.
However, the import statements to remote dependencies might still be red, even though they seem to have been downloaded and appear in the go.mod
file.
Curiously enough, the go project will actually run from the command line, just not from within IntelliJ. Why is this and how can I fix this?
The reason this approach does not work, has to do with the configuration in IntelliJ. That is why it will work from command line, just not in IntelliJ.
There are two ways to fix this:
With the first approach the dependencies defined in your go.mod
file might be re-downloaded, for example when the go project is run from a place where these dependencies are not in the local package registry.
With the second approach the dependencies are put in the 'vendor' folder and do not necessarily need to be re-downloaded, they are sort of made part of the project.