I've just installed Go from the package on the golang site.
When trying to build (or run) import getting an import error, but can't figure out why:
go build world2.go
This produces the error:
# world2.go:3:8: import "world": cannot find package
Running "go env" produces:
GOROOT="/usr/local/go" GOBIN="/usr/local/go/bin" GOARCH="amd64" GOCHAR="6" GOOS="darwin" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64" GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common" CGO_ENABLED="1"
This is what my file structure looks like (ala tree):
. ├── world.go └── world2.go
Any tips what I'm doing wrong or next steps?
Sit back and read up at http://golang.org/doc/code.html#GOPATH it is difficult to grasp at first and isn't like some other languages where you create some packages for use in the same project all at once. Anything you package up is considered to be a publicly fetch-able package, even if you don't.
So you make a 'world' package and set that in your
GOPATH
- something like~/src/mystuff
for stuff that isn't really public, plop all your 'world' package code in there.Then you change your imports statement to something like:
Now the go tools know to search your
GOPATH
for this package code in the~/src/mystuff/world
folder. But it may no be compiled from that src folder yet so run:to have your current project to compile up the
world
package and be ready for linking when you are ready toNow if you wanted others to really use your
world
package you could publish it on somewhere public like github asimport github.com/me/mystuff/world
and then anyone who wanted to build your code could justgo get github.com/me/mystuff/world
or I think evengo get all
now.One last note: in the project folder the
go build
command will look forpackage main
and compile all the files that it is told to, no need to specifygo build world2.go
.g'luck