包下载失败,显示“GOPATH未设置”。为什么会这样?

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

package download fails , "GOPATH not set." why?

问题

操作系统:Ubuntu 12.04

Go版本报告:1.1.1

操作:

我已经配置了.profile文件,包含以下几行代码:

export GOPATH="$HOME/workspace"

export PATH=$PATH:$GOPATH/bin

我通过运行"go env"命令确保它们在go配置中设置。然而,当我尝试运行该命令时,屏幕显示如下图所示:

包下载失败,显示“GOPATH未设置”。为什么会这样?

可能的限制问题:

1)该机器最初安装的是Go v1.0,我将其升级到了go version 1.1.1,不确定这是否有任何影响...但如果存在某种双重配置问题,这可能解释了尽管设置了路径,但为什么它不起作用。

2)我将export命令放在了.profile文件中,但我看到一些帖子指示将其放在.bashrc文件中,无论放在哪个文件中,问题仍然存在。

我需要卸载go 1.0吗?我只是假设版本1.1.1会覆盖它,但这可能是错误的。理想情况下,我想彻底卸载go,然后安装版本1.1.2,但我在golang.org上找不到任何关于卸载的信息,假设卸载可以解决问题。

提前感谢您的帮助。

英文:

OS: Ubuntu 12.04

Go version reporting: 1.1.1

Action:

I have configured the .profile to contain the following lines:

export GOPATH="$HOME/workspace"

export PATH=$PATH:$GOPATH/bin

I have ensured that they are set in the go configuration by running "go env". However when I try to run the command, the screen reports as shown in the image below:

包下载失败,显示“GOPATH未设置”。为什么会这样?

Possible constraining issues:

  1. The box originally had Go v1.0 on it and I upgraded it to go version1.1.1, not sure that should mean anything...but if there is some twin configuration madness at work that may explain the fact it's not working despite the path being set.

  2. I had the export commands in the .profile file but I see some threads indicate to put it in .bashrc, trying in either still gives the same problem.

Do I need to uninstall go 1.0 ? I just assumed version 1.1.1 would over ride it but that could be wrong. Ideally I wanted to uninstall go entirely and then install version 1.1.2 but I couldn't find anything at golang.org on uninstalling assuming that does solve the problem.

Thanks in advance for any assistance.

答案1

得分: 4

正如上面的评论者所说,你不应该在go get命令中使用sudo。当你这样做时,你使用的是root用户的环境(它没有你的GOPATH),并且它创建的任何文件或目录都无法被你的用户编辑。过去,go get命令不会警告没有$GOPATH,所以很容易因此而出错。

要修复你的权限问题,请运行以下命令将所有权更改回你的用户:

sudo chown -R "$USER:" "$GOPATH"

你只需要运行普通的go get命令,因为你可以(也应该)将你的$GOPATH设置为一个你可以控制的目录。确保阅读如何编写Go代码,特别是关于GOPATH的讨论部分。

英文:

As the commenter above stated, you should not use sudo with go get. When you do, you have the root user's environment (which doesn't have your GOPATH) and any files or directories it creates won't be editable by your user. In the past, the go get command would not warn about not having a $GOPATH and so it was easier to get tripped up by this.

To fix your permissions, run the following command to change ownership back to your user:

sudo chown -R "$USER:" "$GOPATH"

You should only ever need to run a plain go get because you can (and should) set your $GOPATH to be a directory you can control. Be sure to read the How To Write Go Code and in particular its discusson on GOPATH.

huangapple
  • 本文由 发表于 2014年1月31日 01:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/21463261.html
匿名

发表评论

匿名网友

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

确定