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 $GO
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
" thego
command looks forsrc/pkg/widget
inside the Go root, and then—if the package source isn't found there—it searches forsrc/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
(orc:\Go
under Windows), but it is possible to install them in a different location.
If you do this, you will need to set theGOROOT
environment variable to that directory when using the Go tools.
To be able to find your project's packages without installing it, (which is common during development), simply append project path to GOPATH
.
GOPATH=$GOPATH:/path/to/project
go run main.go
should be able to find packages from the project itself.Mac
; for Windows
the delimiter is ;
not :
.Goland
:Settings
-> Go
-> GOPATH
, under Project GOPATH
, add project path.Goland
should be able to find packages from the project itself during development.If you have a valid $GOROOT
and $GOPATH
but are developing outside of them, you might get this error if the package (yours or someone else's) hasn't been downloaded.
If that's the case, try go get -d
(-d flag prevents installation) to ensure the package is downloaded before you run, build or install.