设置GOPATH的值

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

GOPATH value setting

问题

我正在安装go,使用的是go1.3.1.windows-amd64.msi安装包。安装完成后,GOROOT是默认设置的,我在PATH中找到了D:\Programs\Go\bin路径。然后我创建了一个GOPATH环境变量。但是在使用"go get"命令时,出现了错误:

package github.com/coreos/etcd: 无法下载,$GOPATH不能设置为$GOROOT。更多详情请参考:go help gopath

操作系统:Windows 7

GOPATH会与GOROOT冲突吗?

我该如何设置这两个路径值?

英文:

I'm install the go with the go1.3.1.windows-amd64.msi, after installation GOROOT is default setting, I found the
D:\Programs\Go\bin in the PATH,then I create a GOPATH environment variant,
when using the 'go get' command, error occurs:

package github.com/coreos/etcd: cannot download, $GOPATH must not be set to $GOROOT. For more details see: go help gopath

OS: windows 7

GOPATH will conflict with GOROOT?

How can I set these two PATH values?

答案1

得分: 32

  • GOROOT 必须指向你安装 Go 的文件夹。
  • GOPATH 必须指向一个空文件夹,它将成为你的工作空间(用于存放项目的 src/pkg/bin 文件夹)。

将这两个变量添加到你的用户环境变量中。

执行 go get github.com/coreos/etcd 应该会:

  • %GOPATH%/src/github.com/coreos/etcd 下下载源代码(src 文件夹会自动创建)
  • %GOPATH%/pkg/windows_amd64 下编译代码(pkg/ 文件夹会自动创建,windows_amd64 反映了你的 Windows 架构)
  • 使用 go install 命令将代码安装到 %GOPATH%/bin 中(bin/ 文件夹也会自动创建)

注意:从 Go 1.8+(2017 年第二季度)开始,默认情况下可能会为你设置 GOPATH(在 Windows 上为 %USERPROFILE%/go,在 Linux 上为 $HOME/go):参见 issue 17262


更新于 2018 年,三年后:随着 Go 1.11 模块 的推出,GOPATH 正在逐渐过时:

mkdir newProject
cd newProject
set GO111MODULE=on
go mod init myproject
英文:
  • GOROOT must reference the folder where you installed GO
  • GOPATH must reference an empty folder which will be your workspace (src/pkg/bin for your projects)

Add those two variables in your user environment variables.

A go get github.com/coreos/etcd should:

  • download the sources in %GOPATH%/src/github.com/coreos/etcd (src is created for you)
  • compile it in %GOPATH%/pkg/windows_amd64 (pkg/ is created for you, windows_amd64 reflects your windows architecture)
  • with go install, install it in %GOPATH%/bin (bin/ is also created for you)

Note: with Go 1.8+ (Q2 2017), GOPATH might be set for you by default to (on Windows) %USERPROFILE%/go.
On Linux, it would be $HOME/go: see issue 17262.


Update 2018, three years later: GOPATH is becoming obsolete with Go 1.11 modules:

mkdir newProject
cd newProject
set GO111MODULE=on
go mod init myproject

答案2

得分: 5

我遇到了同样的问题。然而,我按照教程中所说的设置了一切,但忘记重新启动cmd。所以步骤如下:

  1. 下载并安装Go发行版(GOROOT变量会自动设置)
  2. 在任意位置创建一个新文件夹作为你的工作空间,在其中创建3个目录:binsrcpkg
  3. 然后进入 控制面板 -> 所有控制面板项 -> 系统 -> 高级系统设置 -> 高级 选项卡 -> 环境变量 -> 通过点击 新建系统变量 中添加新的系统变量,变量名为 GOPATH,变量值为 你创建的目录路径
  4. 完成后,重启 你的 cmdBash这很重要),然后你的 GOPATH 就设置好了。为了确认,运行 go env 命令,你将看到你的值。
英文:

I faced with the same problem. However i set everything as it was said in the tutorial but forgot to restart cmd. So the steps were:

  1. Download and install Go distribution(the GOROOT variable was set automatically)
  2. Create new folder wherever you like for your workspace, create there 3 directories: bin, src and pkg
  3. Then go to Control Panel -> All Control Panel Items -> System -> Advansed System Settings -> tab Advanced -> Environment Variables -> add new system variable by clicking New on System varaibles -> Variable name = GOPATH, Variable value = Your:\directory\that\you\created
  4. When you're done, RESTART your cmd or Bash(that's important) and you have your GOPATH set. To be sure run go env and you will see your value.

答案3

得分: 1

你不应该设置$GOROOT

输入export GOROOT=""来解决你的问题。

英文:

You should not set $GOROOT.

Type export GOROOT="" to fix your problem.

答案4

得分: 0

只需要执行 set GOPATH=[路径] 就可以完成你的工作。

英文:

Just set GOPATH=[path] would do your work.

huangapple
  • 本文由 发表于 2014年8月26日 14:48:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/25499670.html
匿名

发表评论

匿名网友

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

确定