go:找不到GOROOT目录:c:\ go

我将GO发行版解压缩到d:\dev\go,将d:\dev\go\bin添加到我的路径中,创建了一个文件d:\Projects\hello\hello.go并将GOPATH设置为d:\Projects\hello。运行Go Build hello.go后则显示:

go: cannot find GOROOT directory: c:\go

现在好几个不同的地方都在强烈敦促我不要设置 GOROOT,设置 GOPATH 才是需要的。

我应该反对这个建议吗?还是我遗漏了什么?

From Dave Cheney's You don’t need to set GOROOT, really article:

There are only two cases that where you may have to set a $GOROOT environment. These are both described in the installation page on the golang.org website. For completeness I will recap them here

  • You are a Linux, FreeBSD or OS X user using the the zip or tarball binary downloads from the golang.org website. These binaries have a $GOROOT value of /usr/local/go and recommend you unpack them into that location. If you choose not to do this, then you must set $GOROOT to the location you chose.
  • You are a Windows user using the zip binary download from the golang.org website. These binaries have a $GOROOT value of C:\Go. If you place Go somewhere else on your system then you must set $GOROOT to the location you chose.

(The rest of the article is a good read if you want to learn the history that has caused confusion over GOROOT, the summary is it's due to outdated instructions mostly from pre-Go1.)

So if you use a pre-built Go binary, and you want to put it somewhere other than where it "wants", then you need GOROOT. Otherwise (i.e. if you build Go from source, or if you put the binary distribution in the default location) you don't need or want to set GOROOT (the above article gives some reasons why not).

If you're unsure, the easiest way to get it "correct" is to first not set anything other GOPATH. Then run go env and see what it says; if that shows reasonable entries for GOROOT and GOPATH then you're done; only set variables to override that if it's wrong.

I set GOROOT to point to the content of that dir:

AUTHORS      PATENTS      api          favicon.ico  misc         src
CONTRIBUTORS README       bin          include      pkg          test
LICENSE      VERSION      doc          lib          robots.txt

That's the dir which containts src, bin, doc, lib, etc...

Maybe the cache problem.

For me, I reopen the MinGW (I installed go in msys2 by pacman), and it worked.

Once Go lang is installed, GOROOT is the root directory of the installation.

When I exploded Go Lang binary in Windows C:\ directory, my GOROOT should be C:\go. If Installed with Windows installer, it may be C:\Program Files\go (or C:\Program Files (x86)\go, for 64-bit packages)

 GOROOT = C:\go