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

10,571

Solution 1

In my case enabling Enable Go modules integration helped.

enter image description here

Solution 2

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"

Solution 3

For any one facing this issue, just tick the "Index entire GOPATH" option in Preferences->Go->GOPATH

Solution 4

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.

Share:
10,571
Dmitry Papka
Author by

Dmitry Papka

Updated on June 05, 2022

Comments

  • Dmitry Papka
    Dmitry Papka about 2 years

    I'm using Idea plugin for Go to work with my project. The structure of my project is the following:

    enter image description here

    controller, entity, model, repository etc - are local packages (where one can use another).

    Unfortunately, Idea can't import one local package from another:

    enter image description here

    enter image description here

    With remote packages everything is just fine.

    My project settings:

    enter image description here

    enter image description here

    What am I doing wrong?

  • Nexonus
    Nexonus over 4 years
    Is it really intended that remote code is under go/src and your own code is under src? Just want to be sure.
  • interestedparty333
    interestedparty333 over 4 years
    Note that this only seems to work on a machine where you have direct access to all of the repos
  • Attila Csipak
    Attila Csipak about 4 years
    You should not link a screenshot uploaded to another site. There's a Image icon for inserting images on the formatting toolbar.
  • TheROX
    TheROX about 4 years
    It helps me on using old (1.7) go version with recent Goland.
  • Yagiz Degirmenci
    Yagiz Degirmenci almost 3 years
    Thanks, i was about to go crazy!
  • landesko
    landesko almost 3 years
    For newer versions of Goland, Go has been moved outside of Languages & Frameworks and moved to the top of the settings menu.
  • Stephen Connolly
    Stephen Connolly over 2 years
    Worked for me. Thank you!