不使用“ go env”中指定的$ GOPATH

I've got

export GOPATH=$HOME/Documents/go

in my .zshrc.

When trying to build a project with the following:

sudo go build -o /usr/bin/

I get a

main.go:5:2: cannot find package "github.com/foo/bar" in any of:
    /usr/local/go/src/github.com/foo/bar (from $GOROOT)
    /Users/JoahJoah/go/src/github.com/foo/bar (from $GOPATH)

I am assuming the "(from $GOPATH)" means the path is whatever I've set my $GOPATH variable to. And

go env

outputs the following:

GOPATH="/Users/Johannes/Documents/go"

The "Documents" part of the path seems to be omitted when using go build.

Is build using a GOPATH from somewhere else?

Since I was running the the command

$ sudo go build -o /usr/bin/

with sudo prefixed, it was running as the root user, causing a different $GOPATH to be used.

As Eric said, I should instead build to /usr/local/bin where sudo can be omitted.

$ go build /usr/local/bin

Works!