英文:
Why can't IntelliJ IDEA import local packages in Go project?
问题
我正在使用Go的Idea插件来处理我的项目。
我的项目结构如下:
controller、entity、model、repository等都是本地包(一个包可以使用另一个包)。
不幸的是,Idea无法导入一个本地包到另一个本地包:
对于远程包,一切都正常。
我的项目设置如下:
我做错了什么?
英文:
I'm using Idea plugin for Go to work with my project.
The structure of my project is the following:
controller, entity, model, repository etc - are local packages (where one can use another).
Unfortunately, Idea can't import one local package from another:
With remote packages everything is just fine.
My project settings:
What am I doing wrong?
答案1
得分: 25
在我的情况下,启用启用Go模块集成有所帮助。
答案2
得分: 12
你需要遵循正确的项目结构-https://golang.org/doc/code.html
基本上,一个名为GOPATH的环境变量应该设置为你的工作区根目录,比如~/dev/go
在$GOPATH/src
目录下存放所有的源代码,例如,当你从github获取一个远程包,比如go get github.com/someone/somepackage
,源代码将会下载到$GOPATH/src/github.com/someone/somepackage
,在.go文件中的导入路径是"github.com/someone/somepackage"
。
你自己的代码也应该存放在$GOPATH/src
下,假设是$GOPATH/src/me/myproject
,那么你的entity和model的导入路径分别是"me/myproject/entity"
和"me/myproject/model"
。
英文:
you need to follow the correct project structure - https://golang.org/doc/code.html
basically, an environment variable called GOPATH should be set to your workspace root, such as ~/dev/go
in $GOPATH/src
all the source code lives, for example, when you get a remote package from github, like go get github.com/someone/somepackage
, the source code will be downloaded to $GOPATH/src/github.com/someone/somepackage
and the import path from within a .go file is `"github.com/someone/somepackage".
your own code should live under $GOPATH/src
as well, let's say it's $GOPATH/src/me/myproject
, then your import path for entity and model are "me/myproject/entity"
and "me/myproject/model"
答案3
得分: 1
对于遇到此问题的任何人,只需在首选项->Go->GOPATH中勾选“索引整个GOPATH”选项。
英文:
For any one facing this issue, just tick the "Index entire GOPATH" option in Preferences->Go->GOPATH
答案4
得分: 1
一旦我更新了首选项 -> 转到 -> GOPATH -> 模块GOPATH,将根目录(即包含src、bin和pkg的目录)包含在其中,同级包的导入就开始正常工作了。虽然“索引整个GOPATH”选项已打开,但似乎没有起到作用。
英文:
Once I updated Preferences -> Go -> GOPATH -> Module GOPATH to include the root directory of my project (i.e. the directory containing src, bin and pkg) the imports of sibling packages started working ok. The "Index entire GOPATH" option was on, but didn't seem to help.
答案5
得分: 0
索引Preferences -> Go -> GOPATH
对我有用。
答案6
得分: -2
请使用GitHub发布页面中的最新版本,这些问题应该已经修复。
谢谢。
英文:
Please use one of the latest releases from github releases page and these kind of issues should be fixed.
Thank you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论