一个好的$GOPATH位置是什么地方?

huangapple go评论84阅读模式
英文:

Where is a good location for $GOPATH?

问题

我正在使用Go语言开发一个项目,需要使用一些外部库,比如MySQL驱动程序、图像处理库等等。目前,我将$GOPATH设置为/usr/lib/go/src,这会将下载的包放在/usr/lib/go/src/src目录下,显然这不正确。如果我将$GOPATH设置为/usr/lib/go,就会出现一个错误,提示不能将$GOPATH设置为与$GOROOT相同的目录。那么,我应该在我的build.sh中设置GOPATH=/path/to/my/project/lib,并在提交到git仓库时将lib/添加到.gitignore文件中吗?

我意识到这可能是一个愚蠢的问题。目前它运行得很好,我只是想知道这是否是一种不好的做法。

英文:

I'm working on a project in Go, which requires several external libraries, like a MySQL driver, an image manipulation library, etc. Right now, I have $GOPATH set to /usr/lib/go/src, which puts any downloaded packages in /usr/lib/go/src/src, which obviously doesn't seem right. If I set $GOPATH to /usr/lib/go, I get an error saying that $GOPATH can't be set to the same directory as $GOROOT. So should I put GOPATH=/path/to/my/project/lib in my build.sh, and when I commit to my git repo, put lib/ in my .gitignore?

I realize this is probably a silly question. It works fine as it is now, I'm just wondering if this is bad practice.

答案1

得分: 8

$GOPATH可以是您选择的任何位置(有一些例外),只要编译器知道在哪里找到它。如果您更改它,请确保使用以下命令更新路径:

export GOPATH=/path/to/gopath

我个人偏好将$GOPATH与我的代码分开,除非我正在编写一个通过go get <repo path>导入的包,这种情况下我会将代码写在:

$GOPATH/src/<repo path>

这是使用go get <repo path>时包存储的标准位置。

英文:

$GOPATH can really be any location you choose (with a few exceptions), so long as the compiler knows where to find it. If you change it just make sure you update the path with

export GOPATH=/path/to/gopath

My personal preference is to keep the $GOPATH separate from my code unless I'm writing a package which is meant to be imported via go get &lt;repo path&gt;, in which case I'll write the code in

$GOPATH/src/&lt;repo path&gt;

which is the standard location that the package gets stored when you use go get &lt;repo path&gt;

huangapple
  • 本文由 发表于 2014年2月14日 09:30:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/21769202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定