英文:
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 <repo path>
, in which case I'll write the code in
$GOPATH/src/<repo path>
which is the standard location that the package gets stored when you use go get <repo path>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论