This article first in my blog, if you feel useful, welcome to like collection, let more friends see.

This article talks about some of my thoughts on Go versioning and introduces you to a small tool, GVM. It’s an easy topic to talk about, but if you want to use it well, you need to comb it out a little bit.

background

Version management of Go, not dependency management of packages. In our daily work, we often do not meet such demand, so we may not understand its value very well.

A few words about the background of my writing this article.

One of the most important announcements about Go in recent weeks was probably the official release of Go 1.13 in September. Check out the Go 1.13 release for updates to see what features are interesting or the official Go 1.13 Relase Notes.

As a Gopher, you can’t keep your restless heart from trying out the new updates as soon as possible. The problem, however, is that switching to a new version of Go often involves some issues, such as the configuration of different versions of the environment, and the compatibility or overwriting of the installed accessibility tools and packages.

Naturally, I wanted to have a solution that would help me switch between Go versions and achieve complete isolation of environments between different versions.

Think about solutions

When it comes to environmental isolation, there are many options, such as multi-host, virtual machine, container, and so on. That all sounds good, and it fulfills the need. But if it’s just for Go versioning, you can do it yourself.

Multi-version switching is mainly the isolation of environment variables of different versions. Before Go 1.10, the variables we cared about were GOROOT, GOPATH, and PATH. After Go 1.10, GOROOT defaults to the current installation PATH for Go, just consider GOPATH and PATH.

Recently, I answered a question about the Go environment variable. Check the answer. The function of each variable is described in detail.

How to implement

Now, I want to switch freely between the two versions of Go on my own computer. How do I do that?

Suppose they are in go1.11/ and go1.13/ under the ~/.goversions/ SDK/directory respectively. I now want to enable go 1.11 by running the following command:

$ exportPATH = ~ /. Goversions/SDK/go1.11 / bin / :$PATH
Copy the code

At this point, GOROOT has automatically identified ~/.goversions/ SDK /go1.11/. Go related toolchains, source code, and standard libraries are all in this directory.

However, in addition to Go, there are other third-party standard libraries, compiled library files and other contents, which are located in GOPATH. If not set, the default is ~/ Go, which will cause confusion when switching between multiple versions. We can set a separate GOPATH for each version.

Such as go1.11, set GOPATH to ~ /. Goversions/GOPATH/go1.11 – global /.

$mkdir ~ /. Goversions/gopath/go1.11 - global / $exportGOPATH = ~ /. Goversions/GOPATH/go1.11 - global /Copy the code

A separate environment is created.

If you want to switch to Go 1.13 now, a few commands will do the job.

$ exportPATH = ~ /. Goversions/SDK/go1.13 / bin / :$PATH$mkdir - pv ~ /. Goversions/gopath/go1.13 - global / $exportGOPATH = ~ /. Goversions/GOPATH/go1.13 - global /Copy the code

The switchover succeeds.

Although, the requirements have been fulfilled, but always feel very uncomfortable to use. For easy operation, you can actually refine the above ideas into shell scripts and organize them into a set of tools.

Are you itching to give it a try?

Unfortunately, this opportunity is no longer available, because someone has already developed a tool that is similar but better than described here: GVM, moovWeb/GVM.

What is a GVM

GVM, Go Version Manager, is the Go Version Manager, which can switch Go versions very lightly. Compared to other languages, there are usually similar tools, such as NVM for NodeJS, Virtualenv for Python, etc.

GVM includes not only the version switch mentioned above, but also the ability to install any version of Go directly from the source editor, preferably 1.5 and later, for reasons explained below.

One awkward point is that GVM was not created to switch between versions of Go. The development team originally developed the tool to solve the dependency problem of the project, by switching the environment to achieve package dependency switch. Below, I’ll show you how to do it.

The problem is that now that Go’s dependency management is getting better and the official Go Module is getting better and better, GOPATH is getting weaker and weaker, and GVM seems to be only useful for helping us quickly experience different versions of Go.

With all that nonsense, let’s start experimenting with the tool.

How to install

Installation is as simple as the following command.

$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
Copy the code

The output shows:

Cloning from https://github.com/moovweb/gvm.git to /home/vagrant/.gvm
which: no go in (/usr/local/bin:/usr/bin:/usr/local/ sbin, / usr/sbin, / home/vagrant/local/bin: / home/vagrant/bin) No existing Go versions detected Installed GVM v1.0.22 Please restart your terminal session or to get started right away run `source /home/vagrant/.gvm/scripts/gvm`
Copy the code

Installation complete!

Restart the console or run source $HOME/.gvm/scripts/ GVM to enable GVM.

Remind, different operating systems also need corresponding dependencies to install, see the introduction of the project description. There’s no mention of Windows, so I don’t know if it’s available.

GVM installation Go

GVM compiles the Go installation by downloading the source code from Github. Versions are based on tags in the source code. Since Go is self-compiled for version 1.5 and later, to install Go using GVM, we need to have the Go environment available in advance.

You can read my previous article about how to install Go in detail, which I think is fairly detailed.

Once Go is installed, you can switch between any version of Go using GVM installation.

$GVM install go1.11Copy the code

Wait until the run is complete.

Depending on your network, the first installation may take a while, as you’ll need to download the source code from Github for the first time.

Check the version

First, check to see what versions of Go are already installed on my system, with the command GVM list.

$ gvm list

gvm gos (installed)

   go111.
   go112.
   go113.
   go113.beta1
Copy the code

With four versions installed, go1.13beta1 is an unstable version, so GVM is handy if we want to try out new features in Go as soon as possible.

In addition to viewing installed versions, you can view all versions via GVM Listall, which comes from the tag tag in the source code.

$ gvm listall

gvm gos (available)

   go1
   go1. 01.
   go1. 02.
   go1. 03.
   go11.. go113.
   go113.beta1
   go113.rc1
   go113.rc2
Copy the code

But this cannot be done on macs, and the GVM implementation uses the Linux sort command, which is incompatible with sort on Macs.

How to solve it?

Install coreutils, which has a qsort command available. Brew Install coreutils can be installed directly. Then, modify the file $HOME /. GVM/scripts/function/tool, amend the sort of them to tree.

Select version

Selecting the enabled version is straightforward. As follows:

$GVM use go1.11 [--default]Copy the code

After the command is successfully enabled, run the Go version and Go env command to check whether the command is enabled. If you want to default a version, add –default.

Package Environment Management

In addition to the Go version management, GVM can also manage the package environment, related commands are pkgenv and pkgset. It’s also handy if you’re not using a package dependency management tool.

As an example, if we want to create a new project blog, we can create the environment ahead of time.

$ gvm pkgset create blog  # to create
$ gvm pkgset use blog     # enable
Copy the code

Now, the packages we install with Go Get will default to the blog environment. The principle is that Go Get places the installation in the first directory in GOPATH by default.

All right, that’s it. Interested friends can study again. After all, after the go Mod, this feature will not be used in the future.

GVM directory structure

GVM is shell written and is installed in the $HOME/. GVM/directory by default. A look at its directory structure will help us understand its implementation.

Some of the main directories are as follows:

archive             # go source
bin                 # GVM executable file
environments        Configure environment variables for different environments
scripts             # GVM subcommand script
logs                # log message
pkgsets             The path where each individual environment gopath is located
Copy the code

After studying the implementation of GVM, we can see that this approach applies to many other tools as well. If we run into the same requirements later, we can implement one ourselves, even if we don’t have the tools in place.

conclusion

This article started with my requirements and led to the topic of how to manage the Go version flexibly.

Experience has taught me that since other languages have tools to fulfill such requirements, Go should, too. I did some searching and found GVM. Although I found some bugs and bad experiences when using it, overall, it was enough to meet my needs.

reference

GVM MoovWeb/GVM GVM + Go mod


Welcome to pay attention to my public number!