英文:
Why does Golang say my GOPATH isn't set when I set it the command directly before?
问题
这变得非常令人沮丧。我在~/.bash_profile中设置了我的GOPATH,但Go仍然说它没有设置。所以我手动设置了它,但它仍然说没有设置。
computer:~ doug$ export GOPATH=~/Dropbox/golang
computer:~ doug$ sudo go get -u golang.org/x/tools/cmd/...
package golang.org/x/tools/cmd/...: 无法下载,未设置$GOPATH。有关更多详细信息,请参阅:go help gopath
我在这里做错了什么?如果我输入goenv,它甚至列出它已经设置为正确的路径。
英文:
This is becoming very frustrating. I set my GOPATH in my ~/.bash_profile, but Go still says it's not set. So I set it manually, and it still says.
computer:~ doug$ export GOPATH=~/Dropbox/golang
computer:~ doug$ sudo go get -u golang.org/x/tools/cmd/...
package golang.org/x/tools/cmd/...: cannot download, $GOPATH not set. For more details see: go help gopath
What am I doing wrong here? If I type goenv it even lists it as being set to the right path.
答案1
得分: 2
sudo默认情况下会删除环境变量,因此以root身份运行的命令无法看到设置为doug的环境变量。一般情况下,你不需要使用sudo来运行go get命令,尽管golang.org/x/tools包中的一些包可能有些特殊。尝试只安装你需要的包(不使用sudo),而不是使用...。
英文:
sudo removes environment variables by default, so the command running as root can't see the environment variable set as doug. In general you shouldn't need sudo to run go get, although some of the golang.org/x/tools packages are somewhat special. Try installing just the ones you need (without sudo) instead of using ...
答案2
得分: 1
我特别喜欢 Go 的一个方面,就是它的整个环境设置非常容易,只需使用 wget 和 tar 命令,无需任何特殊权限。
以下是我通常在任何系统上安装(和更新)Go 的步骤:
$ wget https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz
$ tar -xzvf go1.6.2.linux-amd64.tar.gz
$ export GOROOT=$PWD/go
$ export PATH=$PWD/go/bin:$PATH
$ which go
/tmp/example/go/bin/go
$ go version
go version go1.6.2 linux/amd64
你可以在这里找到 tar 存档的链接:https://golang.org/dl/
我喜欢将我使用的每个版本下载并提取到 $HOME/Programs 目录中,然后只需设置一个符号链接指向我实际想要使用的版本:
ls -la /home/fgrosse/Programs | grep go
lrwxrwxrwx 1 fgrosse fgrosse 6 Mar 9 20:52 go -> go1.6.1
drwxr-xr-x 11 fgrosse fgrosse 4.0K Feb 17 21:47 go1.5.4
drwxr-xr-x 11 fgrosse fgrosse 4.0K Feb 17 21:47 go1.6
drwxrwxr-x 11 fgrosse fgrosse 4.0K Apr 23 19:58 go1.6.1
drwxrwxr-x 11 fgrosse fgrosse 4.0K Apr 23 19:58 go1.6.2
$ echo $GOROOT
/home/fgrosse/Programs/go
英文:
I especially like about go that its entire environment is particularly easy to setup just with wget and tar and without the need for any special permissions.
This is how I usually install (and update) go on any system:
$ wget https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz
$ tar -xzvf go1.6.2.linux-amd64.tar.gz
$ export GOROOT=$PWD/go
$ export PATH=$PWD/go/bin:$PATH
$ which go
/tmp/example/go/bin/go
$ go version
go version go1.6.2 linux/amd64
You can find the links to the tar archives here: https://golang.org/dl/
I like to download and extract each version I use to $HOME/Programs and then just set a symlink to point to the one I actually want to use:
ls -la /home/fgrosse/Programs | grep go
lrwxrwxrwx 1 fgrosse fgrosse 6 Mar 9 20:52 go -> go1.6.1
drwxr-xr-x 11 fgrosse fgrosse 4.0K Feb 17 21:47 go1.5.4
drwxr-xr-x 11 fgrosse fgrosse 4.0K Feb 17 21:47 go1.6
drwxrwxr-x 11 fgrosse fgrosse 4.0K Apr 23 19:58 go1.6.1
drwxrwxr-x 11 fgrosse fgrosse 4.0K Apr 23 19:58 go1.6.2
$ echo $GOROOT
/home/fgrosse/Programs/go
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论