go install 总是尝试使用 GOROOT,而 GOPATH 不在 go env 列表中。

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

go install always attempts to use GOROOT and GOPATH is not listed under go env

问题

我对GOPATH有一个非常令人沮丧的问题,尽管在.profile中设置了,但在调用go env时没有出现,并且似乎没有影响到go install的目标位置。

我尝试使用go install来安装包,但是出现了以下错误,清楚地显示它正在尝试安装在/usr/lib/go而不是预期的目录/home/me/dev/go

$ go install github.com/songgao/colorgo
go install github.com/songgao/go.pipeline: mkdir /usr/lib/go/pkg/linux_386/github.com: permission denied

go env给出以下结果。

$ go env
GOROOT="/usr/lib/go"
GOBIN=""
GOARCH="386"
GOCHAR="8"
GOOS="linux"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_386"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread"
CGO_ENABLED="1"

但它肯定是被设置了的。

$ echo $GOPATH
/home/me/dev/go

**更新:**我已经在~/.profile中导出了GOPATH并进行了资源化,但是我仍然遇到同样的问题。

$ export GOPATH=/home/me/dev/go
$ go env
GOROOT="/usr/lib/go"
GOBIN=""
GOARCH="386"
GOCHAR="8"
GOOS="linux"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_386"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread"
CGO_ENABLED="1"

**再次更新:**自从我升级到go1.1beta2后,这个问题已经消失了。我不确定出了什么问题,因为GOPATH似乎被正确设置了,但现在一切都正常工作了。

英文:

I'm having a quite frustrating problem with the GOPATH, which, despite being set in .profile, is not appearing when invoking go env, and does not appear to be affecting the go install target location.

I'm attempting to use go install to install packages, and am getting this error, which clearly shows that it is attempting to install in /usr/lib/go, rather than the intended directory of /home/me/dev/go.

$ go install github.com/songgao/colorgo
go install github.com/songgao/go.pipeline: mkdir /usr/lib/go/pkg/linux_386/github.com: permission denied

go env gives the following results.

$ go env
GOROOT="/usr/lib/go"
GOBIN=""
GOARCH="386"
GOCHAR="8"
GOOS="linux"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_386"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread"
CGO_ENABLED="1"

But it most certainly is set.

$ echo $GOPATH
/home/me/dev/go

Update: I have exported GOPATH in ~/.profile and sourced it, but I'm still having the same problem.

$ export GOPATH=/home/me/dev/go
$ go env
GOROOT="/usr/lib/go"
GOBIN=""
GOARCH="386"
GOCHAR="8"
GOOS="linux"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_386"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread"
CGO_ENABLED="1"

Update again: This problem has ceased since I upgraded to go1.1beta2. I'm not sure what was wrong, because GOPATH appeared to be set correctly, but it's all apparently working now.

答案1

得分: 4

GOPATH 和工作空间

$ mkdir -p $HOME/dev/go/src
$ mkdir -p $HOME/dev/go/bin

$HOME/.profile 中:

export GOPATH=$HOME/dev/go:
export PATH=$PATH:$HOME/dev/go/bin

然后重新启动或注销并重新登录。

一旦您登录,首先运行:

$ env | grep -i '^GO'
GOPATH=/home/me/dev/go
$ cat $HOME/.profile

您得到什么输出?

英文:

GOPATH and workspaces

$ mkdir -p $HOME/dev/go/src
$ mkdir -p $HOME/dev/go/bin

In $HOME/.profile:

export GOPATH=$HOME/dev/go:
export PATH=$PATH:$HOME/dev/go/bin

Then reboot or log out and log in.

As soon as you have logged in, before anything else, run:

$ env | grep -i '^GO'
GOPATH=/home/me/dev/go
$ cat $HOME/.profile

What output do you get?

答案2

得分: 0

由于您提供了设置GOPATH的证据,您问题的第二部分 - 在go env中未列出GOPATH - 可能是由于您安装的版本的"go env"引起的。 "go1(linux)"和"go1.0.2(windows)"版本的"go env"都不显示GOPATH。如果您查看源代码(GOROOT/src/cmd/go/env.go),您会发现在顶部的要显示的元素列表(mkEnv)中,它从GOROOT开始,不包含GOPATH。

go1.0.3(windows)版本显示GOPATH;然而,源代码中相应的列表从GOARCH开始。

我承认这并不能解决您问题的第一部分 - (尝试)安装到GOROOT - 但也许上述内容可以帮助您集中精力解决问题(构建过程)。

英文:

As you gave evidence for having set GOPATH, the second part of your question - GOPATH is not listed under go env - may be caused by the "go env" of your installed version. Neither go1 (linux) nor go1.0.2 (windows) versions of "go env" display GOPATH. If you look at the source (GOROOT/src/cmd/go/env.go), you'll find right at the top the list of elements to display (mkEnv) which starts with GOROOT and does not contain GOPATH.

Version go1.0.3 (windows) does display GOPATH; the corresponding list in the source, however, starts with GOARCH.

I admit that this does not solve the first part of your problem - (trying to) install into GOROOT - but perhaps the above will help you to focus on the culprit (build process).

答案3

得分: 0

我认为很明显,go install会将所有内容安装到你的GOTOOLDIR中,而GOTOOLDIR也与GOROOT相关:

> 你的: GOROOT="/usr/lib/go"

根据文档

> 安装Go工具
>
> Go二进制发行版假设它们将被安装在/usr/local/go(或Windows下的c:\Go)中,但也可以将它们安装在其他位置。如果你这样做了,在使用Go工具时,你需要将GOROOT环境变量设置为该目录。
>
> 例如,如果你将Go安装到你的主目录,你应该将以下命令添加到$HOME/.profile中:
>
> export GOROOT=$HOME/go
>
> export PATH=$PATH:$GOROOT/bin
>
> Windows用户应该阅读关于在Windows下设置环境变量的部分。

你应该按照这些步骤更改你的GOROOT,而不是GOPATH,然后一切都应该正常工作。

如果还不起作用,请同时更改GOTOOLDIR。

英文:

I think it's quite clear that go install everything in your GOTOOLDIR, GOTOOLDIR is also related with GOROOT:

> Yours: GOROOT="/usr/lib/go"

According to Document:

> Install the Go tools
>
>The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but it is possible to install them in a different location. If you do this, you will need to set the GOROOT environment variable to that directory when using the Go tools.
>
>For example, if you installed Go to your home directory you should add the following commands to $HOME/.profile:
>
>export GOROOT=$HOME/go
>
>export PATH=$PATH:$GOROOT/bin
>
>Windows users should read the section about setting environment variables under Windows.

You should follow these steps to change your GOROOT, instead of GOPATH, then everything should work.

If it's not work, change GOTOOLDIR as well.

答案4

得分: 0

ln -sf /usr/local/go/bin/go /usr/bin/go

英文:

Create a soft link, then copy the go binary in /usr/local as follow:

# ln -sf /usr/local/go/bin/go /usr/bin/go

huangapple
  • 本文由 发表于 2013年4月6日 12:09:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/15846914.html
匿名

发表评论

匿名网友

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

确定