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

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

应用筹备

仅实用于 Mac 的筹备工作

依照MacOSX 要求中的阐明执行以下命令。

xcode-select --installbrew updatebrew 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/.gvmNo existing Go versions detectedInstalled GVM v1.0.22Please 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 helpUsage: gvm [command]Description:  GVM is the Go Version ManagerCommands:  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 listallgvm 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 -BInstalling go1.16.15 from binary sourceERROR: Binary Go unavailable for this platform$ $ gvm install go1.17.5 -B Installing go1.17.5 from binary sourceERROR: 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 --defaultNow 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 listgvm 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 versiongo 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 versiongo version go1.17.5 darwin/arm64$ $ which go/Users/user_name/.gvm/gos/go1.17.5/bin/go

参考

https://github.com/moovweb/gvm