As the error shows, the compiler cannot find
subpack from either
/usr/local/go/src/subpack (from $GOROOT)
where the standard library packages (such as
strings) are, or
/Users/anilbhatia/go/src/subpack (from $GOPATH)
where user installed/defined packages are.
To make the import work, you just need to include the relative path of the
subpack package (relative to
$GOPATH/src) in your
main.go is in
/Users/anilbhatia/go/src/parentpack, then its import should be
If I understand you correctly, you want the caller of
main.go) to be in an unrelated location of
subpack. This actually works out of box. Your
main.go could be located anywhere. When you compile it, the compiler sees the import path of
parentpack/subpack, and goes to
$GOROOT/src to find it.
For more information about the source code organization and some typical examples, you can run
go help gopath
in your shell.