在IntelliJ中,即使启用了go模块集成,导入也不会加载

当将IntelliJ IDEA与Go插件或GoLand一起使用,并使用Go 1.11或更高版本时,可以通过转到首选项->语言和框架-> Go-> Go模块(vgo)并选中“启用Go模块”框来启用Go模块 (vgo)整合”。 当你有一个项目是由适当的初始化 go mod init因而就有一个 go.modgo.sum文件,它将在命令行中使用以下命令下载依赖项 go build or from IntelliJ by pressing the green play icon. 或者按下绿色的play图标就可以得到IntelliJ。 但是,到远程依赖项的导入语句可能仍然是红色的,即使它们似乎已经下载并出现在go.mod文件中

奇怪的是,go项目实际上是从命令行运行的,而不是从IntelliJ内部运行的。为什么是这样的,我该怎么做?

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:

  1. Go into Preferences -> Languages & Frameworks -> Go -> Go Modules (vgo) and make sure the box 'Vendoring mode' is not checked.
  2. Make sure you have a folder called 'vendor' in your IntelliJ project. Now the box 'Vendoring mode' can be safely checked.

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.