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?
$ 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.gosource file is not in a directory calledfoobar.go buildandgo installtry to match directories, not source files.$GOPATHto a valid directory, e.g.export GOPATH="$HOME/go"foobar.goto$GOPATH/src/foobar/foobar.goand building should work just fine.Additional recommended steps:
$GOPATH/binto your$PATHby:PATH="$GOPATH/bin:$PATH"main.goto a subfolder of$GOPATH/src, e.g.$GOPATH/src/testgo install testshould now create an executable in$GOPATH/binthat can be called by typingtestinto your terminal.