當你想為每個項目切換 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.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 help
Usage: gvm [command]Description:GVM is the Go Version ManagerCommands:version - print the gvm version numberget - gets the latest code (for debugging)use - select a go version to use (--default to set permanently)diff - view changes to Go roothelp - display this usage textimplode - completely remove gvminstall - install go versionsuninstall - uninstall go versionscross - install go cross compilerslinkthis - link this directory into GOPATHlist - list installed go versionslistall - list available versionsalias - manage go version aliasespkgset - manage go packages setspkgenv - edit the environment for a package set
如何使用gvm
查看可以安裝的版本
gvm listall
您可以檢查可以安裝哪個版本
$ gvm listallgvm gos (available)go1go1.0.1go1.0.2go1.0.3go1.1go1.1rc2go1.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 listgvm gos (installed)=> go1.16.15go1.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