ARTICLES
Go version manager
By me
Why mutiple versions?
There’s some projects that are kinda tied to specific versions of Go. It’s really not common to see something like this but i have stumble upon a few. And not only that - maybe you want to benchmark/test different versions to see if there’s some improvement. When it come to this you uninstalling and installing different Go version can be painful especially if you need to setup whole environment again.
Old way
- Uninstall or remove all go versions
- Unset GOROOT everywhere
- Clone Go like this:
hg clone https://code.google.com/p/go $HOME/go
- Use cloned repo to split releases by branches like:
hg clone $HOME/go -r go12.1.1 $HOME/go-12.1.1
and thishg clone $HOME/go -r go13.1.0 $HOME/go-13.1.0
, i think you got the idea, just repeat this step for all versions you need - Build separated versions:
cd $HOME/go-12.1.1/src && ./make.bash
… - Set aliases (not really needed, there’s a bunch of way to separate binary like ln -s or simple renaming):
alias go-12.1.1=$HOME/go-12.1.1/bin/go
- Compiling your code:
go-12.1.1 build
where go-12.1.1 is alias we set in previous step
Reverting to older versions can make some repositories incompatible so watch out for those. There’s also possible compatibility problem with system c compiler if you go too old.
Better way - GVM!
There’s no downsides like previous one with this approach. So GVM sounds like NVM (node version manager), tool for managing node versions. Kinda same problem as Go, multiple versions are needed for different projects, guess no language is perfect. So, ok, i said no downsides - this means that every different version have it’s own repository and they won’t interefere with each other in cross-versions, very nice, isn’t it?
Linux approach
Here’s how you can install it: bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
Basically, you just run some unknown code from who-knows-who on your machine. It can be anything from installing very hard to remove malware to god know what. Not in this case, but i will let you think about that during coffee break.
What this script does is:
- Checks dependencies
- Clone GVM from https://github.com/moovweb/gvm.git (SRC_REPO)
- Install Go
- Manage the GOPATH environment (remember i told you this is done by version)
- Setup GVM_ROOT, GVM_DEST, GVM_NAME - some internal vars
There’s some requirements for installing this, you can find it on this link: https://github.com/moovweb/gvm/blob/master/README.md. Keep in mind that toolchain for compiling Go is changed in transition from 1.4 to 1.5. So if you want to compile Go 1.5+ you need Go 1.4. Before Go 1.5 there was C code that need to be compiled and you need to satisfy that requirements too.
After installing GVM there’s additional options:
Usage: gvm install [version] [options]
-s, --source=SOURCE Install Go from specified source.
-n, --name=NAME Override the default name for this version.
-pb, --with-protobuf Install Go protocol buffers.
-b, --with-build-tools Install package build tools.
-B, --binary Only install from binary.
--prefer-binary Attempt a binary install, falling back to source.
-h, --help Display this message.
To list go versions use this
gvm list
To list downloadable versions
gvm listall
Uninstalling gvm and all go versions previously downloaded
gvm implode
Installing works like this
gvm install go1.10.1
Installing go1.10.1...
* Compiling...
go1.10.1 successfully installed!
Now check if everything is ok
gvm list
gvm gos (installed)
go1.10.1
And you’re done!
What about Windows
You’re using Windows to code in Go? Reinstall Linux and follow Linux approach.
Just kidding, it’s ok to code in Windows, i guess?
There’s a GVM repo for Windows: https://github.com/danielkermode/gvm
and you can download releases here: https://github.com/danielkermode/gvm/releases/.
Last version is from 2016, and i didn’t really test this, if you did please let me know in the comment section below.