Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

go: cannot find GOROOT directory: c:\go

Tags:

go

build

I unzipped the Go distribution into d:\dev\go, added d:\dev\go\bin to my path, created a file d:\projects\hello\hello.go and set GOPATH to d:\projects\hello. Running go build hello.go tells me:

go: cannot find GOROOT directory: c:\go

Now several different places strongly urge me not to set GOROOT, setting GOPATH should be everything that is needed.

Should I go against this recommendation or is there something I am missing?

like image 766
AndreKR Avatar asked Jun 27 '15 23:06

AndreKR


1 Answers

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.

like image 123
Dave C Avatar answered Oct 11 '22 03:10

Dave C