背景
在 MAC 上把 Go 版本升級到1.13后, 在終端上執行 go 命令表現得比較奇怪,很不習慣, 想回滾到之前穩定版本。
解決
Go 版本的升降級算是一件也比較麻煩的事情了,1.12版本之前,安裝完 Go 之后, 需要配置 GOROOT 和 GOPATH 等, 跟之前一堆 Go 管理工具像 glide 和 vendor 有得一拼(還好現在官方 go modules 逐漸將包管理入口也做了收斂)。
這里收集和整理了 Go 版本升降級的兩個方法。
方法一
安裝想要的 Go 版本, 並做鏈接
-
檢查當前 Go 版本:
$ go version go version go1.13 darwin/amd64 -
通過 Homebrew 安裝想要的 Go 版本, 版本列表詳見:Homebrew Formulae/go
$ brew install go@1.12 Updating Homebrew... ==> Auto-updated Homebrew! Updated 2 taps (homebrew/core and homebrew/cask). ==> Updated Formulae openssl@1.1 ✔ ==> Downloading https://homebrew.bintray.com/bottles/go@1.12-1.12.9.mojave.bottle.tar.gz Already downloaded: /Users/username/Library/Caches/Homebrew/downloads/6392e5d3faa67a6132d43699cf470ecc764ba42f38cce8cdccb785c587b8bda8--go@1.12-1.12.9.mojave.bottle.tar.gz ==> Pouring go@1.12-1.12.9.mojave.bottle.tar.gz ==> Caveats go@1.12 is keg-only, which means it was not symlinked into /usr/local, because this is an alternate version of another formula. If you need to have go@1.12 first in your PATH run: echo 'export PATH="/usr/local/opt/go@1.12/bin:$PATH"' >> ~/.zshrc ==> Summary /usr/local/Cellar/go@1.12/1.12.9: 9,819 files, 452.8MB -
取消鏈接當前安裝的 Go 版本:
$ brew unlink go Unlinking /usr/local/Cellar/go/1.13... 3 symlinks removed -
鏈接到自定義的 Go 版本:
$ brew link --force go@1.12 Linking /usr/local/Cellar/go@1.12/1.12.9... 3 symlinks created If you need to have this software first in your PATH instead consider running: echo 'export PATH="/usr/local/opt/go@1.12/bin:$PATH"' >> ~/.zshrc因為通過 Homebrew 安裝特定版本的包並不會做自動鏈接, 所有需要需要通過
--force手動鏈接。 -
檢查是否安裝降級(或升級)成功:
$ go version go version go1.12.9 darwin/amd64
方法二(推薦)
在 Github 上找到一個 Go 版本的升級腳本:update-golang, 可以輕松地拉取和安裝新的 Go 版本,基本不會對系統配置做什么修改, 同時提供 remove 方法, 一鍵卸載 Go 環境。
安裝指定版本的 Go:
$ sudo RELEASE=1.13 ./update-golang.sh
卸載 Go 環境:
$ sudo ./update-golang.sh remove
其他食用方式, 查看 README 文檔
