简体   繁体   中英

Why can't IntelliJ IDEA import local packages in Go project?

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?

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"

In my case enabling Enable Go modules integration helped.

在此处输入图片说明

对于遇到此问题的任何人,只需在 Preferences->Go->GOPATH 中勾选“索引整个 GOPATH”选项

Once I updated Preferences -> Go -> GOPATH -> Module GOPATH to include the root directory of my project (ie 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.

Indexing the Preferences -> Go -> GOPATH worked for me.

索引 <code>Preferences -> Go -> GOPATH</code> 对我有用。

Please use one of the latest releases from github releases page and these kind of issues should be fixed.

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM