HiveBrain v1.2.0
Get Started
← Back to all entries
debuggoCritical

Go build: "Cannot find package" (even though GOPATH is set)

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
packageevenfindsetthoughcannotbuildgopath

Problem

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

Solution

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.

Context

Stack Overflow Q#13214029, score: 191

Revisions (0)

No revisions yet.