1. Demand background

In actual development, you will come into contact with different open source projects, and these projects may be developed by different teams, and the go versions used are different.

Take me as an example. Recently I am studying KubeVirt (an open source project that can manage virtual machines under K8S). In order to achieve my own needs, I need to develop KubeVirt twice.

We learned from go.mod that KubeVirt was developed using go 1.13. At the same time, in order to be able to use LVM as a storage method for virtual machines under KubeVirt, we introduced the lvm-csi plug-in developed by Ali. Similarly, this plug-in has been The realized function is still a little bit behind our expectations, so secondary development is also required. Looking at go.mod, I found that lvm-csi was developed under an older version, which is go 1.12.

At the same time, I have installed go 1.14 version on my machine a long time ago, and I have been using it for development.

The question is, how can I install so many versions of golang on my machine at the same time without conflicting each other?

2. Install multiple versions of Go

If you want different versions of go to not conflict with each other, just install them in different directories.

First download the two versions of the installation package

$ wget https://dl.google.com/go/go1.12.linux-amd64.tar.gz
$ wget https://dl.google.com/go/go1.13.linux-amd64.tar.gz

Then unzip to different directories

# 解压go 1.12
sudo tar -C /tmp/ -xzf go1.12.linux-amd64.tar.gz
mv /tmp/go /usr/local/go12 

# 解压go 1.13
sudo tar -C /tmp/  -xzf go1.13.linux-amd64.tar.gz
mv /tmp/go /usr/local/go13

3. Simple and rude solution

Need to pay attention to installing multiple versions of Golang on the same machine

  • Distinguish different versions of go entry
  • Switch to different environment variables

Differentiate different versions of go entry

When you have multiple go in the environment, when you use go build such as 0617d41daf30bd, how does the system know which version you want to use?

The system does not know, so you need to give different names for different versions of go

  • The go entrance of go 1.12 is changed to /usr/local/go12/bin/go12
  • The go entrance of go 1.13 is changed to /usr/local/go13/bin/go13
  • Follow this list

In this way, there will be no confusion

$ go12 version
go version go1.12 linux/amd64
$ go13 version
go version go1.13 linux/amd64

Switch to different environment variables

Different Go entry points are used, and the corresponding environment variables must also be variables, such as GOROOT, which will not automatically switch according to which version of Go you use. See below they all output a value.

$ go12 env GOROOT
/usr/local/go
$ go13 env GOROOT
/usr/local/go

Therefore, this switch between different environment variables has to be done by yourself

Another thing to note is

  1. go 1.12 no go env -w in -w parameters
  2. Although there is -w go 1.13, it cannot overwrite the environment variables at the OS level. You need to unset it before modifying it.

After knowing these points, you can implement the switching plan by yourself.

The solution I think is to write a script to modify environment variables /usr/local/go13/bin/

$ cat << EOF >/usr/local/go12/bin/goto12
unset GOROOT
go env -w GOROOT="/usr/local/go12/"
EOF

$ chmod +x /usr/local/go12/bin/goto12

Next time you want to use go 1.12, you can directly use the following command to modify the environment variable

$ source goto12

go 1.13 is the same, so I dare not repeat it.

The effect achieved is as follows

$ go env GOROOT
/usr/local/go

$ source goto12
$ go12 env GOROOT
/usr/local/go12

$source goto13
$ go13 env GOROOT
/usr/local/go13

4. A better solution than above

At this point, you should have already felt that you have to manually source it every time you switch, which is still a bit troublesome.

In fact, I have a better method, this solution does not need to change the name of the go entry, and also does not even use the source.

Just execute the following two conditional commands (two for each version, two versions are four)

$ cat << EOF >/usr/local/go12/bin/go12
unset GOROOT
go env -w GOROOT="/usr/local/go12/"
/usr/local/go12/bin/go $@
EOF

$ chmod + /usr/local/go12/bin/go12

$ cat << EOF >/usr/local/go13/bin/go13
unset GOROOT
go env -w GOROOT="/usr/local/go13/"
/usr/local/go13/bin/go $@
EOF

$ chmod + /usr/local/go13/bin/go13

If you do, when you execute go12, it will automatically modify the environment variables of go12, and execute go13 will automatically modify the environment variables of go13, and will not affect the default go behavior.

Of course, I only changed the environment variable GOROOT above. If there are other differences in different go versions, you can continue to add the corresponding shell code in go12 or go13.

Talk a bit

I have written a lot of Python-related articles on SegmentFault, including Python utility tools, Python high-efficiency skills, and PyCharm usage skills. I am very happy to get the recognition and support of many friends who know.

With their encouragement, I sorted the past articles into three PDF e-books

PyCharm Chinese Guide

"PyCharm Chinese Guide" uses more than 300 GIF dynamic images to explain in detail 105 PyCharm efficient use skills that are most suitable for actual development. The content is easy to understand and suitable for all Python developers.

Online experience address: https://pycharm.iswbm.com

Python Black Magic Guide

"Python Black Magic Guide" currently ushered in the v3.0 version, which contains more than 100 development tips, which is very suitable for reading fragments in leisure time.

Online experience address: https://magic.iswbm.com

Python Chinese Guide

The best learning material for learning Python is always the official Python documentation. Unfortunately, most of the official documentation is now in English. Although there is a Chinese translation, the progress is really worrying. In order to take care of classmates who are not good at English, I wrote an online Python document for friends with zero foundation - "Python Chinese Guide"

Online experience address: https://python.iswbm.com

It is helpful, remember to help point a praise yo ~


Python编程时光
135 声望26 粉丝