dongyang2229 2013-02-20 19:25
浏览 41
已采纳

在非默认目录中安装Go软件包

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?

  • 写回答

3条回答 默认 最新

  • douou2026 2013-02-21 13:21
    关注

    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/".

    However if I quit Terminal and then open it back up, and run "go env" again, the GOPATH variable is not set.
    

    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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化