Go Version Management

There is a better alternative, please see <<VSC Developing inside a Container>>.

This post talks about how to install multiple go binaries in the same machine and switch back and forth easily.

You may need a primary go version for daily work. Download and install go for different operating system from here:

The instruction will put the download to /usr/local/go, for multi-version install below, they are downloaded to different folder so there is no conflict.

If you need to install other go versions in the same machine:

First, there are 2 go env variables involved:

  • GOROOT is for compiler/tools.
  • GOPATH is for your own go projects and 3rd party libraries (downloaded with go get/install).

You can check their value by:

1
2
go env GOROOT
go env GOPATH

The go install location is controlled by the GOBIN and GOPATH environment variables depends their existence, in the order:

  • If GOBIN is set, binary is installed at that directory.
  • If GOPATH is set, binary is installed at the bin subdirectory of the first path in the GOPATH list, otherwise binary is installed to the bin subdirectory of the default GOPATH ($HOME/go).
1
go install golang.org/dl/go1.10.7@latest

Go to $GOPATH/bin to find the downloaded object and run:

1
go1.10.7 download

From output, 1.10.7 version go binary will be downloaded to $GOPATH/../sdk folder.

To use the downloaded version, we can update the PATH environment variable:

1
export PATH=/usr/local/google/home/<user>/sdk/go1.10.7/bin:$PATH

NOTE that if there are many go versions, must set the target go bin path at the beginning of PATH, the GOROOT will be automatically set according to first go bin in the PATH.

Then verifying by running:

1
2
3
4
5
# go version
go version go1.10.7 linux/amd64

# go env GOROOT is updated changed accordingly to 1.10.7 bin path
/usr/local/google/home/<user>/sdk/go1.10.7

To recover original setting, just revert the PATH environment variable.

0%