英文:
$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"
所以,你的安装可能将其放在了不同的位置,但无论如何,你不能将GOROOT
和GOPATH
设置为相同的路径,原因是你的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
命令时,它会在GOROOT
和GOPATH
两个位置查找,它会发现你的所有导入(如fmt
和os
)在GOROOT
和GOPATH
中都存在,这样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="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.2.1/libexec" # <- its right here
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"
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
± % 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论