Saturday, April 22, 2023

Go virtualenvs (sort of)

Well how do you run multiple versions of the Go compiler on the same sandbox. This isn't quite the same as different virtualenvs for Python - because that is a run-time construct while this is a pure compile-time mechanism. But objectives are analogous. On a given machine, I want to be able to write Go code and build it using different versions of the Go compiler.

Assuming you already have some version of Go compiler installed, and you've set GOPATH (and GOROOT, GOBIN, etc.), here is a way to deploy additional versions of the compiler.

go install golang.org/dl/go1.17.12@latest

The above command downloads an installer binary for Go version 1.17.12 (just a random version) and places it under $GOBIN/. If you now want to install go 1.17.12, you have to run the following command.

$GOBIN/go1.17.12 download

This installs go 1.17.12 side-by-side with other versions of Go that might already be present on the box. Now, run the following command to determine the installation location.

$GOBIN/go1.17.12 env GOROOT

Use the GOROOT for the go1.17.12 installation (or whatever your chosen version is) to set environment variables. Maybe define a .goenv1.17.12 script that you can source in the shell. Each time you want to switch to this version of the Go compiler, source this script. You would need to keep your sources separate for each version I think - I am not sure if you can switch between Go versions on the same repo.

No comments: