20

What is the best practice to install packages (those with go get...) in a local directory?

Example: I'd like to try out the Revel web framework, but I don't want to clutter my go installation at /usr/local/go.

Normally I'd say sudo go get github.com/robfig/revel as written on the home page, but that would install it beneath /usr/local/go/src/pkg/....

Is there an easy way to say (for example) go get --local ... and have the package in the current (sub) directory?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
topskip
  • 16,207
  • 15
  • 67
  • 99
  • Hard to chose which answer to accept. For the moment I've only played with `GOPATH`, so @keks's answer is the one I chose, but this might change in the future :) – topskip Sep 20 '12 at 15:41

3 Answers3

25

To expand on keks answer, you can update your .bashrc to look like this

export GOROOT=/usr/local/go
export GOPATH=~/workspace/me/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Now all packages installed with go get are separate from the go distribution.

dskinner
  • 10,527
  • 3
  • 34
  • 43
14

You can export the env variable GOPATH. For me it is ~/local/lib/go. This folder has the subfolders bin, pkg and src, so it's just like /usr/local/go. The go-tool will then automatically download , build and install packages into this directory.

keks
  • 1,052
  • 1
  • 10
  • 12
10

You might want to consider using Go Version Manager (gvm).

Apart from switching between Go versions easily, it also lets you switch between pkgsets ("workspaces").

First you create a set

gvm pkgset create myproject

and then you use it

gvm pkgset use myproject

Works like a charm.

stephanos
  • 3,319
  • 7
  • 33
  • 47