Installing packages in a local directory

What is the best practice for installing packages (with go get... ) in a local directory?

Example. I would like to try the Revel web framework, but I don't want to clutter the go installation on /usr/local/go .

Normally I would say sudo go get github.com/robfig/revel as written on the home page, but this will install it under /usr/local/go/src/pkg/...

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

+12
go install package local
source share
4 answers

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

+10
source share

To extend the answer to cakes, 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.

+20
source share

You might want to use Go Version Manager (gvm) .

Besides switching between versions of Go, it also allows switching between pkgsets ("workspaces").

First you create a set

 gvm pkgset create myproject 

and then you use it

 gvm pkgset use myproject 

It works like a charm.

+10
source share

In a modern module with go turned on (presented in go 1.11), you can use the gobin program with the GOBIN env variable indicating the purpose of the binary file:

 GOBIN=./local gobin github.com/robfig/revel 

Installing the reel is as follows:

 GO111MODULE=off go get -u github.com/myitcv/gobin 
0
source share

All Articles