简体   繁体   中英

Multiple versions of go

I am trying to learn go-lng following the official docs here: https://golang.org/doc/install

I am stuck on the step installing extra go versions. Apparently this line should install a different version of go and make the executable available in my $PATH but it's not happening:

go get golang.org/dl/go1.10.7

Instead what I see is:

c.craig$ go get golang.org/dl/go1.10.7
c.craig$ go1.10.7 download
-bash: go1.10.7: command not found

Where am I going wrong? I've tried it with a space assuming this was just a typo in the docs but even that doesn't work:

c.craig$ go get golang.org/dl/go1.10.7
c.craig$ go 1.10.7 download
go 1.10.7: unknown command

The binary is installed $HOME/go/bin (or more accurately the bin directory under the path you get from go env GOPATH ). The go get command doesn't update your $PATH , so you need to add the install directory to your $PATH yourself.

After setting the following path variables in.bash_profile (do not forget to run: source ~/.bash_profile)

$ export GOPATH=$HOME/go
$ export GOBIN=$HOME/go/bin

Install go version in one terminal:

$ go get golang.org/dl/go1.13.15
$ go1.13.15 download

Now you also want to install another go version say go1.15.13 by executing the below commands (remember to set the path as told above)

$ go get golang.org/dl/go1.15.13
$ go1.15.13 download

Now you have two go versions installed; 1.13.15 and 1.15.13.

Say, in one terminal, you want to use version 1.13.15 so you can create an alias as below in that terminal window:

$ alias go="go1.13.15"
$ go version
go version go1.13.15 darwin/amd64

In another terminal you can switch to different version

$ alias go="go1.15.13"
$ go version
go version go1.15.13 darwin/amd64

You may wish to try Go Version Manager ( gvm ):

bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
gvm listall
gvm install go1.10.7 [-B] 
gvm use go1.10.7 [--default]
gvm list

In case you want to compile from source and don't have go installed:

gvm install go1.4 -B
gvm use go1.4
#CGO_ENABLED=0
export GOROOT_BOOTSTRAP=$GOROOT
gvm install go1.10.7
gvm use go1.10.7

In case you're getting compiling errors you may wish to uncomment CGO_ENABLED=0 prior to compiling from source.

I faced the same issue when trying to install version 1.17.11, my base version is 1.18.2.

Followed instructions from here https://go.dev/doc/manage-install but it didn't work for me. Then, i realized, after run go install golang.org/dl/go<version>@latest (where <version> is the version you need), a binary file name go<version> will be downloaded in $HOME/go/bin/ .

Move to $HOME/go/bin/ , run ./go<version> download , and version you need will be downloaded at $HOME/sdk/

To use the version you had just downloaded, add this line to the end of ~/.bashrc file:

alias go<version>=$HOME/sdk/go<version>/bin/go

Save and exit. Run source ~/.bashrc to execute it!

I also found useful using this tool to switch between versions: Freshgo - https://nikfot.github.io/freshgo/ It is a go binary and easy to install (linux).

I installs go versions from the source easily and can keep a backup of your go source files so if you switch between versions you save some little time from go pkg downloads.

Easy as that:

freshgo select -v 1.19

I also installed it in my shell to check for latest versions, with a ready script:

echo "${PATH to freshgo repo}/check_ver.sh" >>  ~/.profile

I got the same problem as yours, after I knew that go is back-compatible, I copy the /usr/local/go/bin/go to /usr/local/go/bin/go{version}.

For Mac users it's easier and I guess for Linux users should be similar, you just need to:

  1. use a modern version of GO ie >= 1.12
  2. make sure you can run go version
  3. browse the version you need: https://go.dev/dl/ lets say you want to download go1.13.15 you can run the following command(I think the @latest is optional): go install golang.org/dl/go1.13.15@latest , if nothing happens then everything is ok.
  4. beacuse you need to update your PATH you can either do it or open a new terminal.
  5. run the command: go1.13.15 download
  6. Now you can find a new directory in $HOME/sdk/go1.13.15 thats where your new instalation of GO resides.
  7. update your GOROOT env var to work with your workspace with the following command: export GOROOT=$HOME/sdk/go1.13.15
  8. Finally type go version you should see
go version go1.13.15 darwin/amd64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM