当前位置: 首页 > 工具软件 > smart-go-dl > 使用案例 >

go 安装/版本切换

彭嘉赐
2023-12-01

1. go 安装/版本切换

1.1. 安装 go1.17

今天发现一个新方法可以在同一台机器上安装 go 的不同版本:

$ go get golang.org/dl/go1.17
$ go1.17 download
$ go1.17 version

1.2. brew 安装

我们先使用命令来查找一下 go 版本

brew search go

这是终端会显示所有的查询结果, 这里因为要安装 go1.12, 所以执行以下命令进行安装。

brew install go@1.12

安装成功后, 进行链接以下就可以了。

brew link go@1.12 --force

至此安装结束, 终端输入 go version 进行验证。

1.2.1. brew link unlink

This process would likely apply to other Homebrew formula also.

First search for your desired package:

brew search go

You should get a list of results that include the below. Not “go” is very unspecific so you may get a lot of results:

==> Formulae
go ✔
go@1.10
go@1.11
go@1.9

Then install the desired version:

brew install go@1.10

Remember that you can have more than one package installed at the same time, but you cannot have them all available at the same time. So if you have the latest/generic go package already installed you need to unlink it first:

brew unlink go

And then you can link a different version:

brew link go@1.10

In some cases you may need to link them with the --force and --overwrite options:

brew link --force --overwrite go@1.10

1.3. 版本切换

1.3.1. bash 环境切换

我一般采用这种方法,简单可控:

# Set the GOPROXY environment variable
export GOPRIVATE=gitlab.xxx.com/*
export GOPROXY=https://goproxy.cn,direct

# export GOROOT=/opt/go
export GOROOT=/opt/go1.17.9
export GOBIN=$GOROOT/bin
export GOPATH=/Users/mac/Downloads/project/ent

export GOALL=$GOROOT:$GOBIN:$GOPATH

export PATH=$PATH:$GOALL
 类似资料: