..

Golang-使用gvm进行版本控制

当你想为每个项目切换 go 版本时,gvm (Go Version Manager) 很方便。

这里,我将介绍“如何在Mac上安装gvm”和“如何使用gvm”

使用准备

仅适用于 Mac 的准备工作

按照MacOSX 要求中的说明执行以下命令。

xcode-select --install
brew update
brew install mercurial

gvm安装

我使用 zsh 作为我的 shell。

$ echo $SHELL
/bin/zsh

对于 zsh,您可以这样安装:

$ zsh < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
Cloning from https://github.com/moovweb/gvm.git to /Users/user_name/.gvm
No existing Go versions detected
Installed GVM v1.0.22

Please restart your terminal session or to get started right away run
 `source /Users/user_name/.gvm/scripts/gvm`

~/.zshrc 以下行被添加到最后一行

[[ -s "/Users/user_name/.gvm/scripts/gvm" ]] && source "/Users/user_name/.gvm/scripts/gvm"

重新启动终端 gvm 即可使用

$ gvm help
Usage: gvm [command]

Description:
  GVM is the Go Version Manager

Commands:
  version    - print the gvm version number
  get        - gets the latest code (for debugging)
  use        - select a go version to use (--default to set permanently)
  diff       - view changes to Go root
  help       - display this usage text
  implode    - completely remove gvm
  install    - install go versions
  uninstall  - uninstall go versions
  cross      - install go cross compilers
  linkthis   - link this directory into GOPATH
  list       - list installed go versions
  listall    - list available versions
  alias      - manage go version aliases
  pkgset     - manage go packages sets
  pkgenv     - edit the environment for a package set

如何使用gvm

查看可以安装的版本

gvm listall 您可以检查可以安装哪个版本

$ gvm listall

gvm gos (available)

   go1
   go1.0.1
   go1.0.2
   go1.0.3
   go1.1
   go1.1rc2
   go1.1rc3
   :

安装 go 版本

M1 Mac 我正在使用,但是当我执行以下命令时,出现错误

$ gvm install go1.16.15 -B
Installing go1.16.15 from binary source
ERROR: Binary Go unavailable for this platform
$ 
$ gvm install go1.17.5 -B 
Installing go1.17.5 from binary source
ERROR: Binary Go unavailable for this platform

我在以下方面取得了成功:

$ brew install go
==> Downloading https://ghcr.io/v2/homebrew/core/go/manifests/1.18
######################################################################## 100.0%
    :
$ 
$ gvm install go1.16.15   
Installing go1.16.15...
 * Compiling...
go1.16.15 successfully installed!
$ 
$ gvm use go1.16.15 --default
Now using version go1.16.15
$ 
$ go version          
go version go1.16.15 darwin/arm64

之后,即使我卸载了用brew安装的go,我也能够安装另一个版本的go。

$ brew uninstall go          
Uninstalling /opt/homebrew/Cellar/go/1.18... (11,947 files, 595.3MB)
$ 
$ gvm install go1.17.5   
Installing go1.17.5...
 * Compiling...
go1.17.5 successfully installed!

1.16.15 已安装 1.17.5 ,但为每个版本生成了一个文件夹,如下所示。

$ ls ~/.gvm/gos 
go1.16.15       go1.17.5

切换go版本来使用

我目前正在go1.16.15 使用

$ gvm list

gvm gos (installed)

=> go1.16.15
   go1.17.5
$ 
$ echo $GOPATH
/Users/user_name/.gvm/pkgsets/go1.16.15/global
$ 
$ echo $GOROOT
/Users/user_name/.gvm/gos/go1.16.15
$ 
$ go version
go version go1.16.15 darwin/arm64
$
$ which go
/Users/user_name/.gvm/gos/go1.16.15/bin/go

1.17.5 我会尝试切换到

$ gvm use go1.17.5 --default 
Now using version go1.17.5
$ 
$ gvm list                  

gvm gos (installed)

   go1.16.15
=> go1.17.5

$ 
$ echo $GOPATH
/Users/user_name/.gvm/pkgsets/go1.17.5/global
$ 
$ echo $GOROOT
/Users/user_name/.gvm/gos/go1.17.5
$ 
$ go version
go version go1.17.5 darwin/arm64
$ 
$ which go
/Users/user_name/.gvm/gos/go1.17.5/bin/go

参考

https://github.com/moovweb/gvm