• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Go 安装、多版本管理

武飞扬头像
xyc1211
帮助1

正常单版本go安装方式

1. 官网下载pkg安装

  • 地址
    https://go.dev/doc/install

  • 本机 go 安装目录 GOROOT
    默认是 /usr/local/go

  • 升级
    需要从官网下载pkg安装,pkg会先remove之前的版本然后安装

2. mac brew安装

安装目录在 /usr/local/opt/go

# 查看可用go版本
brew search go

# 安装go
brew install go
brew install go@<version>

# 升级
brew updatebrew upgrade go

多版本安装

方案1:golang/dl

官方的方案:包装器
https://github.com/golang/dl

# 1. 下载包装器 go1.16.13(不是源码), 并生成 go1.16.13可执行文件
	# go install 或 go get 下载
go install golang.org/dl/go1.16.13@latest
go get golang.org/dl/go1.16.13

# 2. 下载源码到 用户目录下的sdk/go1.16.13 。运行包装器的install函数下载 Go源码
go1.16.13 download

# 3. 运行。相当于运行 sdk/go1.16.13/bin/go version
go1.16.13 version

go1.16.13 这个命令,一直都只是一个包装器。
如果你希望新安装的 go1.16.13 成为系统默认的 Go 版本,即希望运行 go 运行的是 go1.16.13,方法有很多:

  1. 将 ~/sdk/go1.16.13/bin/go 加入 PATH 环境变量(替换原来的);
$ export GOROOT=$(go1.16.13 env GOROOT) 
$ export PATH=${GOROOT}/bin:$PATH 

$ go version 
go version go1.16.13 darwin/amd64 
  1. 做一个软连,默认 go 执行 go1.16.4(推荐这种方式),不需要频繁修改 PATH;
  2. 移动 go1.16.4 替换之前的 go(不推荐);

方案1:GVM 管理多个go版本

https://github.com/moovweb/gvm

安装GVM

# 查看shell
echo $0

# 执行
$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)

$ 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/yuchunxu/.gvm
	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/yuchunxu/.gvm/scripts/gvm`

# 配置gvm环境变量
$ source /Users/yuchunxu/.gvm/scripts/gvm

学新通

GVM命令

  help       - display this usage text

打印GVM版本号:
  version    - print the gvm version number 
获取GVM最新代码:
  get        - gets the latest code (for debugging)
完全删除GVM
  implode    - completely remove gvm


选择要使用的GO版本(--default 为永久设置):
  use        - select a go version to use (--default to set permanently)
查看对 Go root 的更改
  diff       - view changes to Go root
安装go版本
  install    - install go versions
卸载GO版本
  uninstall  - uninstall go versions
  cross      - install go cross compilers
将此目录设置为 GOPATH
  linkthis   - link this directory into GOPATH
列出已安装的GO版本
  list       - list installed go versions
列出可用的GO版本
  listall    - list available versions
管理Go版本别名
  alias      - manage go version aliases
管理Go包集合
  pkgset     - manage go packages sets
编辑Go包集合的环境
  pkgenv     - edit the environment for a package set
学新通

问题

macOS Big Sur(版本11.0)后无法利用 GVM 進行第一次安裝 Go

  • 报错信息
gvm install go1.4

Downloading Go source...
Installing go1.4...
 * Compiling...
ERROR: Failed to compile. Check the logs at /Users/yuchunxu/.gvm/logs/go-go1.4-compile.log
ERROR: Failed to use installed version
$ gvm install go1.4 -B

Installing go1.4 from binary source
ERROR: Binary Go unavailable for this platform

  • 原因
    因为 download_binary()中的版本检查代码不能正确处理11.0.x,导致“BINARY GO UNAvailable For This Platform”错误消息并退出。

  • 解决方案:用其他方法首次安装go

    1. 先安裝官網的 Go 版本後,利用 GVM 安裝你想要的版本後,刪除原先安裝官網版本。
    2. 使用 brew 安裝 Go 之後,一樣利用 GVM 安裝你想要的版本後,再用 brew uninstall go 來解除安裝。
gvm install 编译出错
gvm install go1.4

Downloading Go source...
Installing go1.4...
 * Compiling...
ERROR: Failed to compile. Check the logs at /Users/yuchunxu/.gvm/logs/go-go1.4-compile.log
ERROR: Failed to use installed version
  • /Users/yuchunxu/.gvm/logs/go-go1.4-compile.log
Building Go cmd/dist using /usr/local/go.
go tool dist: cannot invoke C compiler "clang": exit status 1

Go needs a system C compiler for use with cgo.
To set a C compiler, set CC=the-compiler.
To disable cgo, set CGO_ENABLED=0.

Command output:

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
  • 原因
    缺少 CommandLineTools,不可以在命令行中运行C程序

  • 解决方案: 安装 CommandLineTools
    xcode-select --install 或者 下载 CommandLineTools安装

  • 验证

gcc -v

其他没实操过的问题

  • Go 1.5 从工具链中删除了 C 编译器,并用 Go 编写的编译器取而代之。显然,如果您还没有安装有效的 Go,这会产生引导问题。为了编译 Go 1.5 ,请确保首先安装 Go 1.4。
gvm install go1.18.2

Downloading Go source...
Installing go1.18.2...
 * Compiling...
ERROR: Failed to compile. Check the logs at /Users/yuchunxu/.gvm/logs/go-go1.18.2-compile.log
ERROR: Failed to use installed version
  • 1.4安装失败
    Go 版本 1.4.x 在这一点上是古老的,不能在 Big Sur 上运行(它已经有将近 6 年的历史了),但最近的二进制版本可以按预期工作。
    Go版本在1.4以上,需要在指令最后加上-B

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgekhhc
系列文章
更多 icon
同类精品
更多 icon
继续加载