Tagged as: golang

Are you getting the error: 'import "fmt": cannot find package'? The error is quite easy to solve with a simple change to the GOPATH environment variable.

When setting up go (golang), and running go build on the hello world file, you may experience the error:

hello.go:3:8: import "fmt": cannot find package
package .
    imports runtime: import "runtime": cannot find package

It may also manifest as something like the text below if you use go run.

go build fmt: exec: "/home/user/go/pkg/tool/linux_386/pack": stat /home/user/go/pkg/tool/linux_386/pack: no such file or directory
go build runtime: exec: "/home/user/go/pkg/tool/linux_386/pack": stat /home/user/go/pkg/tool/linux_386/pack: no such file or directory

Your problem if you encounter these errors, is that your GOROOT is set to what your GOPATH should be set to. If you unset your go root and set your GOPATH:

export GOPATH=$GOROOT
unset GOROOT

Your file should work (go run hello.go):

Hello, world.

You may also want to add the GOPATH env variable to your .bashrc and delete the GOROOT variable if it's there.