24

I use IntelliJ IDEA as golang IDE in Windows , and system environment have already set GOPATH,but IDEA can't load $GOPATH.

I use go get github.com/astaxie/beego, this package is installed correctly, but in IDEA can't import this library.

I have added this package in project's structure in this way:

enter image description here

and this project's choice “Go sdk go1.1.1 windows/amd64 ”like enter image description here

how can I set GOPATH in idea?

Kenenbek Arzymatov
  • 8,439
  • 19
  • 58
  • 109
fuhao715
  • 475
  • 2
  • 5
  • 9
  • please have a look at the answer here: http://stackoverflow.com/a/27939351/951414 – dlsniper Jan 14 '15 at 10:48
  • 0.9.15 is a very old version of the plugin, right now the plugin has a stable and full featured version in the plugin repository, please try it: https://plugins.jetbrains.com/plugin/5047 – ignatov Nov 16 '15 at 21:20

6 Answers6

50

Update (2017-10-22)

According to the release notes of the new version of Gogland (EAP 16), the corresponding Go plugin can only be used with IntelliJ IDEA Ultimate from now on. This follows the pattern of providing support for different languages either via a plugin for IntelliJ IDEA Ultimate, or as a standalone IDE, such as PyCharm, Webstorm, or in this case, Goland.

For more details see the related blog post.

Update (2017-04-24)

As an alternative solution that ultimately deals with the OP's problem, I would like to recommend the new IDE specifically for Go development, JetBrains Gogland. The setup is pretty much effortless, but in case you would have difficulties, there is already heplful documentation provided.

Check Gogland, might work for you.

There is also an official plugin that "has everything you could find in Gogland".

In case you are wondering what could be the difference between this new plugin, and the old go-lang-idea-plugin, check the FAQ article with the explanation.

Original answer

Background info

On OS X Mavericks, Go installed via Macports. Using Go plugin version 0.9.15 with IntelliJ IDEA.

Problem

I wanted to make the IDE understand the imports and provide autocomplete for any package I have installed in GOPATH. (I started creating an app using Revel and were missing the autocomplete...)

Solution

Here is what I did:

  • Configure the Go SDK (In menu: View > Open Library Settings > Platform Settings > SDKs).
  • Open the "Classpath" tab of the Go SDK you want to set up.
  • Add a new item, select the 'src' directory in your $GOPATH.
  • Click 'Apply' or 'OK'.
  • (not working!?!?, grumble, headache...)
  • In menu: File > Invalidate Caches / Restart...
  • Click 'Invalidate and Restart'.
  • WIN!

Hope this might be what you were looking for!

Emixam23
  • 3,854
  • 8
  • 50
  • 107
Temaruk
  • 733
  • 10
  • 15
  • This works, and I am soooo glad I found your answer. But there is something that's maybe bothersome, when you create something with revel it goes with everything else in you go path. Which means that if I follow your instructions of adding the src folder, my own app gets given the dependency color of files and folders. Did you also work around that? – Hugo Mar 06 '14 at 23:12
  • Thanks. It works! I've been using Atom.io IDE for all my golang projects because I was getting too annoyed by IntelliJ IDE. With this trick I'm back to my productivity. One more question, is there a way to force the go plugin to lint and format the source code upon save? The atom.io plugin does a grate job on that. – Ali Oct 05 '14 at 01:17
  • There is a quick fix for that answer and using alt + cmd + L – Ali Oct 05 '14 at 01:35
  • This did not fix the problem for me. The libraries that are tucked away in my godeps folder are invisible to intellij. – Shadoninja Jun 09 '16 at 16:29
  • Yes it's working. Thank you. Additional clue. Let's say you have project and i name the folder test. The folder hierrarchy structure should be like this. src/github.com/instance/test. Thats what i do with my local development – Faris Rayhan Feb 03 '18 at 07:03
18

This is where you set GOPATH in IntelliJ 2016 (in my case my GOPATH is /development/workspace/golang)

enter image description here

Arun Gopalpuri
  • 2,340
  • 26
  • 27
4

Try the latest version of the plugin. It should work better since 0.9.15 release.

And feel free to help us out or report any bugs you encounter.

dlsniper
  • 7,188
  • 1
  • 35
  • 44
  • 1
    Does this address all of the OPs questions? Particularly _"how can I set GOPATH in idea?"_ –  Apr 24 '15 at 00:32
  • @MickyDuncan the OP is asking for something that's related to a deprecated version, and all the other answers advise him to to wrong things in the context of the latest versions. So, if you ask me, saying: please update to fix the issue you describe is a valid answer :) – dlsniper Apr 24 '15 at 16:14
3

IDEA plugin for Go does not understand GOPATH as it is defined by Go conventions, it uses only GOROOT, which is determined from SDK. The best workaround is to make symbolic links in GOROOT to packages in GOPATH:

$GOPATH/src/github.com       <--  $GOROOT/src/pkg/github.com
$GOPATH/src/code.google.com  <--  $GOROOT/src/pkg/code.google.com
... etc

It should be possible to do in Windows too.

Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
1

I don't use IntelliJ, but i have a similar issue with Eclipse. I'm probably doing something wrong but Eclipse does not recognize the GOPATH env variable by itself.

What I do is adding manually to the Go settings an entry to the library i am using. For example it would looks like /somewhere/in/your/hd/github.com/astaxie/beego. Note that you have to specify the base directory of your library, not the /src or /pkg folder.

Hope this helps.

sigu
  • 76
  • 1
  • 2
1

In your system setting make sure all these enviroment variables are set in this way

GOROOT=C:\Go
GOBIN=%GOPATH%\bin
PATH=%PATH%;c:\go\bin;%GOBIN%

(If you have installed Go in c:\Go)

In your user folder create a directory for Go, named for example goplayground
Create another environment variable that points to this directory

GOPATH=C:\Users\YOUR_USENAME\goplayground

That's all. Now IntelliJ IDEA Go plugin should work correctly.

Silverstorm
  • 15,398
  • 2
  • 38
  • 52