[Go] 使用 gvm 安裝與管理不同的 Go 版本

[Go] 使用 gvm 安裝與管理不同的 Go 版本

之前使用 Go 都是自己安裝想要的版本

不過都得自己去裝,漸漸的也覺得有些不方便…

 

查了一下,發現蠻多人是使用 gvm (Go Version Manager),

來管理不同版本的 Go,就來試試看吧~

 

在 Mac 上,Homebrew 找不到 gvm,

於是用 gvm 提供的 bash 指令安裝:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
Cloning from https://github.com/moovweb/gvm.git to /Users/testuser/.gvm
macOS detected. User shell is: /bin/bash
Created profile for existing install of Go at "/usr/local/go"
Installed GVM v1.0.22
Please restart your terminal session or to get started right away run
`source /Users/testuser/.gvm/scripts/gvm`
$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer) Cloning from https://github.com/moovweb/gvm.git to /Users/testuser/.gvm macOS detected. User shell is: /bin/bash Created profile for existing install of Go at "/usr/local/go" Installed GVM v1.0.22 Please restart your terminal session or to get started right away run `source /Users/testuser/.gvm/scripts/gvm`
$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)

Cloning from https://github.com/moovweb/gvm.git to /Users/testuser/.gvm
macOS detected. User shell is: /bin/bash
Created profile for existing install of Go at "/usr/local/go"
Installed GVM v1.0.22

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

 

裝好之後,在新的 terminal 裡面就可以執行 gvm 指令了:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ gvm
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
applymod - apply the go version in go.mod
$ gvm 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 applymod - apply the go version in go.mod
$ gvm

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
  applymod   - apply the go version in go.mod

 

用  

gvm list
gvm list 列出目前有的 Go 版本…

不過它只列出了 system 這個系統的 (目前版本是 1.20),

我事實上自己還有裝了 go1.18,看起來它是不會列入管理的,

可能因為它不是用 gvm 裝的?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ gvm list
gvm gos (installed)
system
$ gvm list gvm gos (installed) system
$ gvm list

gvm gos (installed)

   system

 

執行  

gvm listall
gvm listall 列出 gvm 目前支援的所有 Go 版本:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ gvm listall
gvm gos (available)
go1
go1.0.1
......
go1.18
go1.18beta1
go1.18beta2
go1.18rc1
go1.18.1
go1.18.2
go1.18.3
go1.18.4
go1.18.5
go1.18.6
go1.18.7
go1.18.8
go1.18.9
go1.18.10
go1.19
go1.19beta1
go1.19rc1
go1.19rc2
go1.19.1
go1.19.2
go1.19.3
go1.19.4
go1.19.5
go1.19.6
go1.19.7
go1.19.8
go1.19.9
go1.19.10
go1.19.11
go1.19.12
go1.20
go1.20rc1
go1.20rc2
go1.20rc3
go1.20.1
go1.20.2
go1.20.3
go1.20.4
go1.20.5
go1.20.6
go1.20.7
go1.21rc1
go1.21rc2
go1.21rc3
go1.21rc4
go1.21.0
$ gvm listall gvm gos (available) go1 go1.0.1 ...... go1.18 go1.18beta1 go1.18beta2 go1.18rc1 go1.18.1 go1.18.2 go1.18.3 go1.18.4 go1.18.5 go1.18.6 go1.18.7 go1.18.8 go1.18.9 go1.18.10 go1.19 go1.19beta1 go1.19rc1 go1.19rc2 go1.19.1 go1.19.2 go1.19.3 go1.19.4 go1.19.5 go1.19.6 go1.19.7 go1.19.8 go1.19.9 go1.19.10 go1.19.11 go1.19.12 go1.20 go1.20rc1 go1.20rc2 go1.20rc3 go1.20.1 go1.20.2 go1.20.3 go1.20.4 go1.20.5 go1.20.6 go1.20.7 go1.21rc1 go1.21rc2 go1.21rc3 go1.21rc4 go1.21.0
$ gvm listall

gvm gos (available)

   go1
   go1.0.1
   ......
   go1.18
   go1.18beta1
   go1.18beta2
   go1.18rc1
   go1.18.1
   go1.18.2
   go1.18.3
   go1.18.4
   go1.18.5
   go1.18.6
   go1.18.7
   go1.18.8
   go1.18.9
   go1.18.10
   go1.19
   go1.19beta1
   go1.19rc1
   go1.19rc2
   go1.19.1
   go1.19.2
   go1.19.3
   go1.19.4
   go1.19.5
   go1.19.6
   go1.19.7
   go1.19.8
   go1.19.9
   go1.19.10
   go1.19.11
   go1.19.12
   go1.20
   go1.20rc1
   go1.20rc2
   go1.20rc3
   go1.20.1
   go1.20.2
   go1.20.3
   go1.20.4
   go1.20.5
   go1.20.6
   go1.20.7
   go1.21rc1
   go1.21rc2
   go1.21rc3
   go1.21rc4
   go1.21.0

 

像是我想安裝 go1.18.10,就用  

gvm install
gvm install 指令,

我另外加了  

-B
-B 參數,這樣就會直接使用 binary 安裝,

避免它去抓 Go 的原始碼從頭開始編譯,可能會很久:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
gvm install go1.18.10 -B
gvm install go1.18.10 -B
gvm install go1.18.10 -B

 

裝好之後再來看一下,已經有在列表內了:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ gvm list
gvm gos (installed)
go1.18.10
system
$ gvm list gvm gos (installed) go1.18.10 system
$ gvm list

gvm gos (installed)

   go1.18.10
   system

 

使用的方式,是執行  

gvm use <version>
gvm use <version> ,

這只會切換當前終端機的 Go 版本:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ gvm use go1.18.10
Now using version go1.18.10
$ gvm use go1.18.10 Now using version go1.18.10
$ gvm use go1.18.10

Now using version go1.18.10

 

看一下 Go 的版本和相關環境變數,確實是 1.18 的:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ go version
go version go1.18.10 darwin/amd64
$ export | egrep GO
declare -x GOPATH="/Users/testuser/.gvm/pkgsets/go1.18.10/global"
declare -x GOROOT="/Users/testuser/.gvm/gos/go1.18.10"
$ go version go version go1.18.10 darwin/amd64 $ export | egrep GO declare -x GOPATH="/Users/testuser/.gvm/pkgsets/go1.18.10/global" declare -x GOROOT="/Users/testuser/.gvm/gos/go1.18.10"
$ go version

go version go1.18.10 darwin/amd64

$ export | egrep GO
declare -x GOPATH="/Users/testuser/.gvm/pkgsets/go1.18.10/global"
declare -x GOROOT="/Users/testuser/.gvm/gos/go1.18.10"

 

也可以切換回系統預設的 Go 版本:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ gvm use system
Now using version system
$ go version
go version go1.20.7 darwin/amd64
$ export | egrep GO
declare -x GOPATH="/Users/testuser/.gvm/pkgsets/system/global"
declare -x GOROOT="/usr/local/go"
$ gvm use system Now using version system $ go version go version go1.20.7 darwin/amd64 $ export | egrep GO declare -x GOPATH="/Users/testuser/.gvm/pkgsets/system/global" declare -x GOROOT="/usr/local/go"
$ gvm use system
Now using version system

$ go version
go version go1.20.7 darwin/amd64

$ export | egrep GO
declare -x GOPATH="/Users/testuser/.gvm/pkgsets/system/global"
declare -x GOROOT="/usr/local/go"

 

這樣就可以隨時切換不同的 Go 版本囉~

(本頁面已被瀏覽過 320 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料