1. Demand background
The actual development will involve different open source projects, which may have been developed by different teams with different versions of Go.
In my case, I have recently been working on KubeVirt (an open source project that can manage virtual machines under K8S). In order to fulfill my own needs, I need to re-develop KubeVirt.
We learned from Go.mod that KubeVirt is developed using Go 1.13, and at the same time, in order to use THE STORAGE mode of LVM in the virtual function under KubeVirt, we also introduced the plug-in of LVM-CSI developed by Ali. Similarly, This plug-in is still a little short of what we expected, so it’s going to have to be redeveloped as well. A look at Go.mod reveals that LVM-CSI was developed under an older version, go 1.12.
At the same time, I had the go 1.14 version installed on my machine a long time ago and have been using it for development.
The question then became, how can I install so many versions of Golang on my machine at the same time without conflict?
2. Install multiple versions of Go
To keep different versions of GO from colliding, simply install them in different directories.
Start by downloading the two versions of the installation packages
$Wget HTTP: / / https://dl.google.com/go/go1.12.linux-amd64.tar.gz
$Wget HTTP: / / https://dl.google.com/go/go1.13.linux-amd64.tar.gz
Copy the code
Then unzip them to different directories
#Unpack the go 1.12Sudo tar -c/TMP / -xzf go1.12.linux-amd64.tar.gz mv/TMP /go /usr/local/go12
#Unpack the go 1.13Sudo tar -c/TMP / -xzf go1.13.linux-amd64.tar.gz mv/TMP /go /usr/local/go13Copy the code
3. Simple and crude solutions
Installing multiple versions of Golang on the same machine requires caution
-
Distinguish between different versions of go entry
-
Switch to using different environment variables
Distinguish between different versions of go entry
When you have multiple Go’s in your environment, how does the system know which version you want to use when you use commands like Go Build?
The system doesn’t know, so you need different names for different versions of Go
- The go portal for Go 1.12 will be changed
/usr/local/go12/bin/go12
- The go portal for Go 1.13 will be changed
/usr/local/go13/bin/go13
- This column on
That way, there’s no confusion
$ go12 versionGo version go1.12 Linux/amd64$ go13 versionGo version go1.13 Linux/amd64Copy the code
Switch to using different environment variables
Different go entries are used, and the corresponding environment variable is also variable, such as GOROOT, which does not automatically switch depending on which version of GO you use. See below, they all output the same value.
$ go12 env GOROOT
/usr/local/go
$ go13 env GOROOT
/usr/local/go
Copy the code
Therefore, this switch between different environment variables has to be done by itself
Another thing to note
- 1.12 no go
go env -w
In the-w
parameter - Go 1.13 does
-w
However, os-level environment variables cannot be overwritten. You must unset them before modifying them
With these points in mind, you can implement your own switching scheme.
In /usr/local/go13/bin/, you can write a script that changes the environment variables
$ cat << EOF >/usr/local/go12/bin/goto12
unset GOROOT
go env -w GOROOT="/usr/local/go12/"
EOF
$ chmod +x /usr/local/go12/bin/goto12
Copy the code
The next time you want to use Go 1.12, you can modify the environment variables directly by using the following command
$ source goto12
Copy the code
The same is true for Go 1.13.
The result 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
Copy the code
4. Better solutions than above
By this point, you should have felt the need to manually source each switch, which is still a bit troublesome.
In fact, I have a better solution. In this solution, I don’t need to change the name of the go entry, nor even the source.
Simply execute the following two conditional commands (two for each version, four for each version)
$ 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
Copy the code
If one, when you execute go12 it will automatically change the environment variables of GO12, and if you execute go13 it will automatically change the environment variables of GO13 without affecting the default go behavior.
Of course, I only changed the GOROOT environment variable above, but if there are other differences between versions of Go, you can continue to add the corresponding shell code in GO12 or GO13.
Ramble about the
I have written many Python related articles on Nuggets, including Python utilities, Python Efficiency Tips, and PyCharm. I am very glad to have received the recognition and support from many Zhihu friends.
With their encouragement, I sorted past articles into three PDF e-books
PyCharm Chinese Guide
PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide PyCharm Chinese Guide
Experience online at pycharm.iswbm.com
Python’s Guide to Dark Magic
The Python Guide to Dark Magic is now available on V3.0, packed with over 100 development tips and tricks that are perfect for reading in fragments at leisure.
Online experience: magic.iswbm.com
Python Chinese Guide
The best source for learning Python is always the official Python documentation. Unfortunately, most of the official Python documentation is in English. Although there is a Chinese translation, the progress is rather slow. In order to take care of students who are not good at English, I wrote an online Python document for those who have no basic Knowledge of Python – the Python Chinese Guide.
Experience online at python.iswbm.com
Please give me a thumbs up if ** is helpful