7

再看GOPATH

 3 years ago
source link: http://blog.studygolang.com/2013/01/%e5%86%8d%e7%9c%8bgopath/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
再看GOPATH

再看GOPATH

2013年1月29日 徐新华 Comments 2 条评论 133,315 人阅读

原本不打算介绍GOPATH,然而,总是有初学者问一些关于GOPATH的问题,因此在这里再介绍一下GOPATH

GOPATH环境变量用于指定这样一些目录:除$GOROOT之外的包含Go项目源代码和二进制文件的目录。go install和go 工具会用到GOPATH:作为编译后二进制的存放目的地和import包时的搜索路径。

GOPATH是一个路径列表,也就是可以同时指定多个目录。多个目录在Mac和Linux下通过”:”分割;Windows下通过”;”分割。注意,大部分情况下会是第一个路径优先,比如:查找包

对于有些情况,比如,在GOPATH所在的路径之外执行go install会怎么样,可以自己试验一下。

个人建议GOPATH中别设置多个路径,甚至不显示指定GOPATH。可参照《Go项目的目录结构》

有些地方建议在设置了GOPATH之后,将$GOPATH/bin加入PATH中,这样可以方便的运行go install好的二进制程序。然而,当存在GOPATH中存在多个路径时,这种写法只会将最后一个路径跟上bin。在mac或linux下可以通过这种方式解决:
${GOPATH//://bin:}/bin

欢迎关注我的公众号:

polarisxu-qrcode-soso-s.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK