从源代码构建项目

How can I build a Go project from source, instead of using go get domain.com/dir/project? For example, instead of

go get github.com/kr/godep

I want to build from the source:

git clone https://github.com/kr/godep.git
cd godep
GOPATH=/tmp/godep go build

The commands above will result in

dep.go:4:2: cannot find package "code.google.com/p/go.tools/go/vcs" in any of:
    /usr/local/Cellar/go/1.2/libexec/src/pkg/code.google.com/p/go.tools/go/vcs (from $GOROOT)
    /Users/hanxue/Source/godep/godep/src/code.google.com/p/go.tools/go/vcs (from $GOPATH)
save.go:5:2: cannot find package "github.com/kr/fs" in any of:
    /usr/local/Cellar/go/1.2/libexec/src/pkg/github.com/kr/fs (from $GOROOT)
    /Users/hanxue/Source/godep/godep/src/github.com/kr/fs (from $GOPATH)

Note: go 1.2 is installed in /usr/local/Cellar/go/1.2 with a link from /usr/local/Cellar/go/1.2/bin/go to /usr/local/bin/go

You need the GOPATH configured correctly. Sometimes a project doesn't have to be checked out in the "sub path" it expects, but often it does and certainly things that depend on it will expect to find it there. So instead of "go get" you can

mkdir -p /tmp/go/src
export GOPATH=/tmp/go
cd $GOPATH/src
mkdir -p github.com/kr/godep
cd github.com/kr/godep/..
git clone http://github.com/kr/godep.git
cd godep
go build

... now rinse and repeat for each dependency!

cd $GOPATH/src
mkdir -p code.google.com/p/
cd code.google.com/p
hg clone https://code.google.com/p/go.tools/

Yes, the vcs dependency was in "go.tools" and needed to be cloned with hg instead. It took a bit of web browsing to figure out. Okay, I think you can see why that's annoying to do by hand.

I'll leave the rest of the dependencies as an exercise for the reader, or you can just use "go get". :-)

A bonus tip that might be what you are really looking for: After checking out the first project, you can use "go get" in that directory to download the dependencies of the project. Sometimes if you have something that's not "go get'able" that's useful if the dependencies are.