在非默认目录中安装Go包

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

Installing Go packages in non-default directories

问题

我在Go中创建了一个API,通过go get安装了两个包,并为自己创建了四个其他包。一切似乎都正常工作。然而,我想将包的位置移动到我的Git目录中,以便我可以将所有内容放在一个目录中。

根据我所了解的,只需要将我的GOPATH变量更改为新目录,并再次运行go get进行测试即可。这样正确吗?如果是的话,我有做错什么吗?下面是在终端(Mac OS X)上运行go env时显示的内容。如果有其他有用的信息,请告诉我。谢谢。

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Applications/MAMP/htdocs/git/cbi/api"
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CGO_ENABLED="1"

我应该澄清,默认情况下我的GOPATH是一个空字符串。我尝试将其更新为上面的目录,因为我希望新的包安装在那里,并且Go在那里查找我创建并放置的任何包。

我通过运行以下命令设置我的GOPATH变量:

export GOPATH="/Applications/MAMP/htdocs/git/cbi/api"

这样做后,当我输入go env时,我可以看到变量已设置。然而,如果我退出终端然后重新打开它,并再次运行go envGOPATH变量就没有设置。它为什么没有保存?我应该以另一种方式设置它,而不是使用export命令吗?

英文:

I've created an API in Go in which I've installed two packages via go get and have created four others for my own use. Everything seems to work fine. However I would like to move where the packages are so that I can have everything within my Git directory.

From what I've read, it should be as easy as changing my GOPATH variable to the new directory, and running go get again to test. Is this correct? If it is, am I doing anything wrong? Below is what shows in the terminal (Mac OS X) when I run go env. If any other info would be useful, please let me know. Thanks.

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Applications/MAMP/htdocs/git/cbi/api"
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CGO_ENABLED="1"

I should clarify that by default my GOPATH was an empty string. I tried updating it to the directory above since that is where I wish new packages be installed, and also for Go to look for any packages I've created an placed there.

I'm setting my GOPATH variable by running:

export GOPATH="/Applications/MAMP/htdocs/git/cbi/api"

After doing this, I can see the variable has been set when I type go env. However if I quit Terminal and then open it back up, and run go env again, the GOPATH variable is not set. Is there a reason it's not saving? Should I be setting it another way instead of export?

答案1

得分: 3

如果您没有设置GOPATH变量,包将被安装在GOROOT中,这是不推荐的。如果您想将GOPATH设置为"/Applications/MAMP/htdocs/git/cbi/api",请确保您的代码目录放在"/Applications/MAMP/htdocs/git/cbi/api/src/"中。

然而,如果我退出终端然后重新打开它,并再次运行"go env",GOPATH变量将未被设置。

就像Jeremy所说的,您应该在bash配置文件中设置环境变量。在Mac OS中,将以下内容添加到您的$HOME/.profile中:
export GOPATH=/Applications/MAMP/htdocs/git/cbi/api

顺便说一下,您可以指定多个GOPATH,例如:export GOPATH=/path/to/gopath1:/path/to/gopath2

英文:

If you don't set the GOPATH variable, the packages will be installed in the GOROOT which is not recommended. If you want to set GOPATH to "/Applications/MAMP/htdocs/git/cbi/api", make sure the directory of your code is put in "/Applications/MAMP/htdocs/git/cbi/api/src/".
<pre>
However if I quit Terminal and then open it back up, and run "go env" again, the GOPATH variable is not set.
</pre>
Like jeremy says, you should set the environment variable in bash configure file. In mac os, add this to your $HOME/.profile :
export GOPATH=/Applications/MAMP/htdocs/git/cbi/api .
BTW, you can specify multiple GOPATH, for exmple, export GOPATH=/path/to/gopath1:/path/to/gopath2

答案2

得分: 2

我的方法是通过将以下内容放入项目目录中的env.sh文件来启动一个新的Go项目:

export GOPATH=$PWD
export PATH=$GOPATH/bin:$PATH

然后在开始在新的终端窗口中工作之前,我总是使用'source'命令来执行这个文件,即:

. env.sh

然后当我使用'go get'命令时,它会将所有下载的源代码放入当前项目文件夹中,我可以将其提交到我选择的版本控制系统中。这种方法的优点是:所有内容都保存在一个目录树中,可以轻松进行版本控制。

如果我忘记执行env.sh文件,Go命令通常会立即失败,因为我没有对/usr/local/go文件的写访问权限,这是它们默认放置的位置。所以我很快就发现我不会忘记执行这个文件。

英文:

My approach has been to start a new Go project by putting the following into env.sh in my project dir

export GOPATH=$PWD
export PATH=$GOPATH/bin:$PATH

Then I always 'source' this file before I start work in a new terminal window, i.e.

. env.sh

Then when I use 'go get', it puts all the downloaded source code into the current project folder and I can commit it to my chosen version control system. This is the advantage of this approach: everything is held in one directory tree which can easily be versioned.

If I should happen to forget to source env.sh, Go commands usually fail immediately because I don't have write access to /usr/local/go files, which is the default place for them to be put. So I soon found I didn't forget.

答案3

得分: 1

请确保GOPATH下有三个子文件夹:src、pkg/linux_amd64(这个与操作系统有关)、bin

更多细节可以参考:GOPATHBuild

英文:

Please make sure GOPATH have three subfolder:src,pkg/linux_amd64(this one has to do with the OS),bin

More details can refer: GOPATH and Build

huangapple
  • 本文由 发表于 2013年2月21日 03:25:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/14988259.html
匿名

发表评论

匿名网友

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

确定