Even though I have GOPATH properly set, I still can't get "go build" or "go run" to find my own packages. What am I doing wrong? Thank you so much!
$ echo $GOROOT
/usr/local/go
$ echo $GOPATH
/home/mitchell/go
$ cat ~/main.go
package main
import "foobar"
func main() { }
$ cat /home/mitchell/go/src/foobar.go
package foobar
$ go build main.go
main.go:3:8: import "foobar": cannot find package
It does not work because your foobar.go
source file is not in a directory called foobar
. go build
and go install
try to match directories, not source files.
- Set
$GOPATH
to a valid directory, e.g. export GOPATH="$HOME/go"
- Move
foobar.go
to $GOPATH/src/foobar/foobar.go
and building should work just fine.
Additional recommended steps:
- Add
$GOPATH/bin
to your $PATH
by: PATH="$GOPATH/bin:$PATH"
- Move
main.go
to a subfolder of $GOPATH/src
, e.g. $GOPATH/src/test
go install test
should now create an executable in $GOPATH/bin
that can be called by typing test
into your terminal.
Edit: since you meant GOPATH, see fasmat's answer (upvoted)
As mentioned in "How do I make go find my package?", you need to put a package xxx
in a directory xxx
.
See the Go language spec:
package math
A set of files sharing the same PackageName
form the implementation of a package.
An implementation may require that all source files for a package inhabit the same directory.
The Code organization mentions:
When building a program that imports the package "widget
" the go
command looks for src/pkg/widget
inside the Go root, and then—if the package source isn't found there—it searches for src/widget
inside each workspace in order.
(a "workspace" is a path entry in your GOPATH
: that variable can reference multiple paths for your 'src, bin, pkg
' to be)
(Original answer)
You also should set GOPATH
to ~/go, not GOROOT
, as illustrated in "How to Write Go Code".
The Go path is used to resolve import statements. It is implemented by and documented in the go/build package.
The GOPATH
environment variable lists places to look for Go code.
On Unix, the value is a colon-separated string.
On Windows, the value is a semicolon-separated string.
On Plan 9, the value is a list.
That is different from GOROOT
:
The Go binary distributions assume they will be installed in /usr/local/go
(or c:\Go
under Windows), but it is possible to install them in a different location.
If you do this, you will need to set the GOROOT
environment variable to that directory when using the Go tools.
TL;DR: Follow Go conventions! (lesson learned the hard way), check for old go versions and remove them. Install latest.
For me the solution was different. I worked on a shared Linux server and after verifying my GOPATH
and other environment variables several times it still didn't work. I encountered several errors including 'Cannot find package' and 'unrecognized import path'. After trying to reinstall with this solution by the instructions on golang.org (including the uninstall part) still encountered problems.
Took me some time to realize that there's still an old version that hasn't been uninstalled (running go version
then which go
again... DAHH) which got me to this question and finally solved.
Have you tried adding the absolute directory of go to your 'path'?
export PATH=$PATH:/directory/to/go/