错误:“无法下载,$GOPATH未设置。”

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

Error "cannot download, $GOPATH not set."

问题

设置:

看一下我的配置:

$ echo $GOPATH && ls -r $GOPATH
/home/cyrus/.go
src  pkg  bin

$ echo $GOROOT && ls $GOROOT
/usr/local/go
api  AUTHORS  bin  CONTRIBUTORS  doc  favicon.ico  include  lib  LICENSE  misc  PATENTS  pkg  README  robots.txt  src  test  VERSION

你可以看到我已经设置了$GOPATH的路径。此外,我还创建了一些可能不需要的子目录。

问题:

为什么下面的命令会生成这个错误信息?

$ go get code.google.com/p/go-tour/gotour                                                                                                               
package code.google.com/p/go-tour/gotour: cannot download, $GOPATH not set. For more details see: go help gopath
英文:

Setup:

Have a look at my configuration:

$ echo $GOPATH && ls -r $GOPATH
/home/cyrus/.go
src  pkg  bin

$ echo $GOROOT && ls $GOROOT
/usr/local/go
api  AUTHORS  bin  CONTRIBUTORS  doc  favicon.ico  include  lib  LICENSE  misc  PATENTS  pkg  README  robots.txt  src  test  VERSION

You can see that I've set a path for $GOPATH. In addition, I've created subdirectories that I may not need.

Question:

Why does the following command generate this error message?

$ go get code.google.com/p/go-tour/gotour                                                                                                               
package code.google.com/p/go-tour/gotour: cannot download, $GOPATH not set. For more details see: go help gopath

答案1

得分: 32

如果你像这样设置一个变量:

GOPATH=$HOME/go

它不会被导出到任何子进程中。它只对该进程可用。如果你想要将它导出到子进程中,使用 export

export GOPATH

你也可以将赋值和导出结合起来:

export GOPATH=$HOME/go
英文:

If you set a variable like this:

GOPATH=$HOME/go

It won't be exported to any subprocesses. It's only available to that process. If you want to export it to subprocesses, use export:

export GOPATH

You can also combine the assignment and export:

export GOPATH=$HOME/go

答案2

得分: 1

我的$GOROOT目录是以所有者:root组:wheel创建的,而不是我作为所有者和管理员作为组。我猜我在不应该使用sudo时使用了sudo(或者某个工具替我使用了sudo)。因此,go get无法将包写入$GOROOT,因此出现了$GOPATH未设置权限被拒绝的错误。

通过在我的$GOROOT上使用chownchgrp(使用-R获取子文件夹),'go get...'可以工作。在我的情况下,VS Code现在能够安装我想要的Go组件。

英文:

My $GOROOT directory was created with owner: root and group: wheel instead having me as the owner and admin as the group. I'll guess that I used sudo when I shouldn't have (or some tool did it for me). As such go get could not write the packages to $GOROOT and hence the $GOPATH not set and permission denied errors.

By using chown and chgrp on my $GOROOT (with -R to get subfolders) 'go get...' worked. In my case VS Code was now able to install the Go components I was after.

答案3

得分: 1

当您运行go env时,您应该能够看到您的GOPATH是否已设置。
在我的情况下,当我在终端上运行此命令时,问题得到解决。

export GOPATH=/usr/local/bin
英文:

When you run go env, you should be able to see if your GOPATH has been set.
On my case though, this command solves it when I had it run on my terminal.

export GOPATH=/usr/local/bin

答案4

得分: 0

我已经设置了GOPATH,并且使用命令go env正确显示。我必须使用chmod为go目录提供正确的权限,并通过go get github.com/constabulary/gb/...安装gb

英文:

i had setup GOPATH and it showed correctly with command go env. i had to give the correct permissions to the go directory using chmod and install gb by go get github.com/constabulary/gb/....

答案5

得分: 0

首先执行`go env'并检查GOROOT路径。然后设置GOPATH。
在我的情况下,我需要将GOPATH设置为/usr/lib/go。之前我尝试过/usr/share/go和/usr/bin/go,但都没有成功。

英文:

First do a `go env' and check the path GOROOT. Accordingly set the GOPATH.
In my case, it had to set GOPATH to /usr/lib/go. Earlier i tried it with /usr/share/go and /usr/bin/go but it didn't work.

答案6

得分: 0

$GOPATH应该是您当前的工作区,即项目所在的文件夹。
或者只需cd ~/go并尝试运行这些命令。
或者您可以在ubuntu的~/.bashrc或~/.bashrc_profile中更新$GOPATH。

英文:

$GOPATH should be your current workspace , a folder where your project resides.
or just cd ~/go and try to run those commands.
Or you can just update $GOPATH in ~/.bashrc or ~/.bashrc_profile on ubuntu

huangapple
  • 本文由 发表于 2013年7月26日 11:06:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/17872223.html
匿名

发表评论

匿名网友

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

确定