3
votes

I installed go 1.7.4 by untarring it into ~/go and setting GOROOT accordingly. I also created ~/work/src/github.com/user/hello and set GOPATH accordingly.

When trying the hello world example from https://golang.org/doc/install#testing I get the following error:

$ go install github.com/user/hello
    ../work/src/github.com/user/hello/hello.go:3:8: cannot find package "fmt" in any of:
    /home/user/go/src/pkg/fmt (from $GOROOT)
    /home/user/work/src/fmt (from $GOPATH)
package github.com/user/hello
    imports runtime: cannot find package "runtime" in any of:
    /home/user/go/src/pkg/runtime (from $GOROOT)
    /home/user/work/src/runtime (from $GOPATH)

Checking ~/go, I find fmt in ~/go/src/fmt and not ~/go/src/pkg/fmt

It feels like I am missing something crucial. Thankful for assistance.

2
Did you install Go from a package manager before? What does go version print? Old versions of Go used to look for stdlib in src/pkg.Ainar-G
Indeed I had an old version of go installed that I was not aware of. Removed it and now it works. Thanks!user3207230
Also: Never set GOROOT, it is not needed to compile a Go program with the go tool.Volker
@Volker But if I installed go in my home directory, I have to set GOROOT, right? I tried unsetting it, but 'go install' failed in that case.user3207230
@user3207230: yes, putting a precompiled go release in a non-standard path is the only case where you should set GOROOT. Just don't forget you have it set if you try running Go from another location, since that is a very common cause of beginner issues, and the reason the default advice is to not set GOROOT.JimB

2 Answers

7
votes

If you installed Go from a package manager before, you might have an old version. Check go version and remove the old version if needed.

2
votes

For MacPorts users with e.g. go 1.10.3 installed, it didn't seem to be correctly installed for me. The runtime standard packages were missing. I even tried uninstalling and reinstalling a clean copy. So I just removed it and got the official darwin binary package from google:

https://golang.org/dl/