$GOPATH不能设置为$GOROOT,为什么?

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

$GOPATH must not be set to $GOROOT, why not?

问题

我在我的Linux机器上安装了Go,路径是/usr/local/go,并打算在/usr/local/go/src上进行开发。然而,当我尝试这样做时,出现了以下错误:

$GOPATH不能设置为$GOROOT

根据我的理解,这意味着你不应该在/usr/local/go上进行开发。为什么呢?

英文:

I've installed Go on my Linux machine in /usr/local/go, and intended to use /usr/local/go/src for development. However when I try this I get the following error:

$GOPATH must not be set to $GOROOT

Effectively as I understand it, this means you shouldn't use /usr/local/go for development. Why not?

答案1

得分: 31

因为/usr/local/go/src已经包含了标准库的代码,你应该将自己的代码与之分开存放。

我知道,其他开发工具可能不会有这个问题,但Go在某些方面要求更加严格。这可能是与标记未使用的变量或导入错误的哲学相同 - 避免一开始看起来很小的问题,但可能会在将来带来更大的麻烦。

英文:

Because /usr/local/go/src already contains the code for the standard library, and you should keep your own code separate from that.

I know, other development tools would have no problem with that, but Go is a little more strict in some ways. It's probably the same philosophy that lies behind flagging unused variables or imports as errors - avoiding problems which may seem small at first, but can lead to bigger headaches in the future.

答案2

得分: 17

将以下内容添加到您的.bashrc文件中:

export GOPATH="${HOME}/workspace"
export GOROOT="${HOME}/go"
export PATH="${GOPATH}/bin:${PATH}"

然后加载~/.bashrc文件:$ source .bashrc

英文:

Add following lines to your .bashrc file:

export GOPATH="${HOME}/workspace"
export GOROOT="${HOME}/go"
export PATH="${GOPATH}/bin:${PATH}"

Then load the ~/.bashrc file: $ source .bashrc

答案3

得分: 11

根据go help gopath的说明:

> GOPATH必须设置才能获取、构建和安装位于标准Go目录之外的包。

因此,GOROOT设置了标准库的位置,而GOPATH用于非标准库。一个库不应同时位于这两个位置。

英文:

From go help gopath:

> GOPATH must be set to get, build and install packages outside the
> standard Go tree.

Thus, GOROOT sets the location of standard library, and GOPATH is for nonstandard libraries. One library should not be at both locations at once.

答案4

得分: 6

这个目录可能是你的$GOROOT所在的位置,但你可以通过go env命令进行确认,它会列出GOROOT作为环境变量之一:

$ go env 
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.2.1/libexec" # <- 就在这里
GOTOOLDIR="/usr/local/Cellar/go/1.2.1/libexec/pkg/tool/darwin_amd64"
TERM="dumb"
CC="clang"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fno-common"
CXX="clang++"
CGO_ENABLED="1"

所以,你的安装可能将其放在了不同的位置,但无论如何,你不能将GOROOTGOPATH设置为相同的路径,原因是你的GOPATH也有一个src文件夹,其中包含了标准库:

@ /usr/local/Cellar/go/1.2.1/libexec/src/pkg
± % ls                                                                    
archive   container errors    hash      log       path      strconv   text
bufio     crypto    expvar    html      math      reflect   strings   time
builtin   database  flag      image     mime      regexp    sync      unicode
bytes     debug     fmt       index     net       runtime   syscall   unsafe

之所以不能将它们放在同一个位置,是因为当你运行go install命令时,它会在GOROOTGOPATH两个位置查找,它会发现你的所有导入(如fmtos)在GOROOTGOPATH中都存在,这样Go编译器就会感到困惑并开始对你大喊大叫(就像在你的情况下发生的那样)。

英文:

This directory may be where you $GOROOT is, but you can always check via go env, and this will list GOROOT as one of the environment variables:

$ go env 
GOARCH=&quot;amd64&quot;
GOBIN=&quot;&quot;
GOCHAR=&quot;6&quot;
GOEXE=&quot;&quot;
GOHOSTARCH=&quot;amd64&quot;
GOHOSTOS=&quot;darwin&quot;
GOOS=&quot;darwin&quot;
GOPATH=&quot;&quot;
GORACE=&quot;&quot;
GOROOT=&quot;/usr/local/Cellar/go/1.2.1/libexec&quot; # &lt;- its right here
GOTOOLDIR=&quot;/usr/local/Cellar/go/1.2.1/libexec/pkg/tool/darwin_amd64&quot;
TERM=&quot;dumb&quot;
CC=&quot;clang&quot;
GOGCCFLAGS=&quot;-g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fno-common&quot;
CXX=&quot;clang++&quot;
CGO_ENABLED=&quot;1&quot;

So, your installation might have it in a different place, but either way you cannot make the same path for both GOROOT and GOPATH, the reason being is that your GOPATH also has a src folder, which houses the standard library:

@ /usr/local/Cellar/go/1.2.1/libexec/src/pkg
&#177; % ls                                                                    
archive   container errors    hash      log       path      strconv   text
bufio     crypto    expvar    html      math      reflect   strings   time
builtin   database  flag      image     mime      regexp    sync      unicode
bytes     debug     fmt       index     net       runtime   syscall   unsafe

The reason why you can't have them in the same place is because when you run go install it will look in both GOROOT and GOPATH, and it will see that all your imports like fmt and os are present in both GOROOT and GOPATH, and thus the poor go compiler will be confused and start shouting at you (which it did in your case).

答案5

得分: 1

你只需要初始化它,而无需再次设置GOPATH,它就可以正常工作。只需运行以下命令:

go mod init 你的项目名称

然后你就可以使用go get或者运行你的go代码,没有任何问题。

英文:

You must initialize it and it will work just fine without in the need to set GOPATH all over again, just run:

go mod init your-project-name

then you can use go get or run your go code with no issues at all

huangapple
  • 本文由 发表于 2014年4月5日 15:08:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/22877775.html
匿名

发表评论

匿名网友

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

确定