您的位置:首页 > 运维架构

Can not import $GOPATH package on Intellij Idea

2014-09-01 22:11 253 查看
最近新买了电脑重新安装了一遍软件,真是够麻烦的。以后服务器还是用docker来玩吧 要不换个服务器,还真是得累死。

在重装的过程中遇到了一个问题 Can not import $GOPATH package on Intellij Idea 

以前解决过 但是没有记录 导致我这回又折腾了好一会才能好 所以还是记录一下吧

首先Go 的环境自己安装好 Goroot Gopath一个都不能少,这里就不说如何安装Go了 网上大把的教程

我分别针对以下几种情况给出对应的解决方法 基本上是囊括了所有Gopath中package无法导入的情况

首先你要确保你在sdk中引用了Goroot 和 Gopath如图所示

1 是一张图 自己看吧 还少了个Gopath的错误提示 因为截图晚了  自动消失了 



解决方案在这里 请戳
链接  这个官方文档说的清清楚楚了 感觉没啥好讲的 但还是给大概讲一下吧

在osx上如果看到了上面的错误提示那么就要在/etc/launchd.conf中设定Gopath和Goroot /etc/launchd.conf默认是没有的需要自己创建

sudo vim /etc/launchd.conf  然后将下面两段代码写入launchd.conf中 因为权限问题sudo

setenv GOROOT /usr/local/go

setenv GOPATH /Users/yourname/go

之后  执行  source /etc/launchd.conf 就ok了 如果碰到 setenv returns "Command not found" 那么将上面两段代码改成下面两段代码

launchctl setenv GOROOT /usr/local/go

launchctl setenv GOPATH /Users/yourname/go

然后 source /etc/launchd.conf

2 插件并不使用Gopath 所以需要在 Goroot 作个 symbol link 

链接在这里  其中我下面贴出的这一段是重点

Looking through the source, it seems that the plugin doesn't use the $GOPATH (to be fair, I think earlier Go docs didn't make the distinction between using $GOPATH and $GOROOT quite as clear as they do now).

In setting up the plugin you have to 

1.) create symlinks for all of the imported package directories in your $GOPATH:

a. $GOROOT/src/pkg/ -> $GOPATH/src

example. from within $GOROOT/src/pkg, ln -s $GOPATH/src/github.com

b. $GOROOT/pkg/target/ -> $GOPATH/pkg/target/

example. from within $GOROOT/pkg/darwin_amd64, ln -s $GOPATH/pkg/darwin_amd64/code.google.com

2.) you MUST set up a source directory when creating the project (however, it can be an empty string, which will then cause the wizard to prompt you that it will set the project root as the source directory)

作个符号链接就好了 但是我并没有碰到这种情况可能是早起插件的bug  但是也收集一下吧 

3 最好一种比较坑爹 是由于idea的缓存问题  即使你重启idea都没用

在确保什么都配置好了的情况下 按照如下操作

File > Invalidate Caches / Restart 点击 restart 如下图所示

这时在试着去import Gopath中的package时就可以正常引用了

如果还有其他情况 还请补充
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐