“启用Go模块集成”在Intellij IDE中的作用是什么?

huangapple go评论72阅读模式
英文:

What does Enable Go modules integration do in Intellij IDE

问题

在没有关于如何在终端中从零开始创建项目的先前知识的情况下,我创建了一个文件夹,进入其中,然后运行了go mod init my_project_name命令,它为我创建了一个go.mod文件,然后我创建了一个名为main.go的文件,构建成功。

然后我创建了一个文件夹,并在其中添加了一个带有与目录相同的包名的go文件,并在其中创建了一个结构体。接下来,我尝试在主包中导入该包,但是当我尝试在终端上构建时,它给了我这个错误:

go: cannot determine module path for source directory /Users/berkcan/workspace/go/my_project_name (outside GOPATH, module path must be specified)

在搜索了一番后,我无法找到解决我的问题的方法,于是我将项目导入了我心爱的Intellij IDE,并启用了Go模块集成,然后一切都正常工作。起初,我以为IDE在构建项目时进行了一些魔法操作,但是即使我在终端上尝试使用go build命令,它也能构建成功。但是我在项目结构中看不到任何差异,也没有在go.mod文件中看到新的一行。

所以发生了什么?当我勾选了Go模块集成选项时,Intellij IDE做了什么?我如何在终端上启用它而不使用Intellij IDE?

英文:

Not having previous knowledge about creating a project from zero within terminal, I've created a folder, cd into of it then run go mod init my_project_name which created a go.mod file for me, then I've created main.go file which built just ok.

Then I created a folder and add a go file inside with package name (being same with directory) and create a struct inside of it. Next I tried to import that package in main package but when I try to build on terminal it gave me this error

go: cannot determine module path for source directory /Users/berkcan/workspace/go/my_project_name (outside GOPATH, module path must be specified)

After googling and not being able to find a solution to my problem, I've imported project to beloved Intellij IDE and I enabled Go modules integration then everything worked flawlessly. First I thought IDE doing some magic inside while building project but even when I try go build command in terminal, it built. But I cant see difference in project structure or a new line in go.mod file.

So what happened, what did Intellij IDE did when I ticked go module integration box and what I can do enable it on terminal without Intellij IDE ?

here is the photo of option in IDE when ticked

答案1

得分: 4

IntelliJ IDEA加上Go插件或GoLand在底层有两种模式来获取有关您的包的信息(简化):

  • GOPATH。IDEA扫描您的$GOPATH目录以构建内部索引,并提供代码补全、解析等功能。
  • Go模块。IDEA执行go list -m -json来解析您的依赖项,并扫描您的$GOPATH/pkg/mod目录(GOMODCACHE的默认值)以获取包。如果它们不存在,IDEA会执行go mod download。完成这些操作后,IDE提供所有内置功能,如代码补全、导航等。

这两种模式都不会更改您的Go或环境变量,也不会更改终端中的行为。当您在设置中勾选“启用Go模块集成”选项时,IDE只是从扫描$GOPATH切换到执行go list并从Go模块缓存中解析您的依赖项。

总之,IntelliJ IDEA并没有进行任何魔法操作。我认为这与您在终端中的自定义Go环境变量有关,特别是GO111MODULE,如果您没有将这些变量传递给GUI应用程序(例如,在.zshrc文件中指定并通过桌面入口运行IDE而不是终端),IntelliJ IDEA将不会继承它们。您可以比较本地终端和IDE内置终端(View | Tool Windows | Terminal)中的go env输出,并找出差异。

英文:

IntelliJ IDEA plus Go plugin or GoLand under the hood has two modes to get the information about your packages (simplified):

  • GOPATH. IDEA scans your $GOPATH directory to build internal indexes of your packages and provides code completion, resolving, etc.
  • Go Modules. IDEA executes go list -m -json to resolve your dependencies and scans your $GOPATH/pkg/mod directory (default value of GOMODCACHE) for the packages. If they don't exist, IDEA executes go mod download. After these operations, the IDE provides all built-in features like code completion, navigation and so on.

Both modes don't change your Go or environment variables as well as behavior in the terminal. When you check Enable Go Modules integration option in the settings, the IDE just switches the mode from scan $GOPATH to execute go list and resolve your dependencies from the Go Modules cache.

To summarize, IntelliJ IDEA doesn't do any magic. I suppose it relates to your custom Go environment variables inside the terminal, especially GO111MODULE and if you didn't pass these variables to the GUI apps (e.g. you have specified it in .zshrc file and run the IDE via Desktop entry instead of the terminal), IntelliJ IDEA doesn't inherit them. You can compare go env output inside your local terminal and built-in inside the IDE (View | Tool Windows | Terminal) and find differences.

huangapple
  • 本文由 发表于 2021年8月16日 17:22:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/68800268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定