在 macOS 上是否有一种方便地切换 Go 版本的方法?

huangapple go评论68阅读模式
英文:

Is there any way to switch go version handily in macOS?

问题

在 macOS 上切换 Go 版本有没有好的方法?

如果使用 Python,我们可以定义 python2、python2.7、python3、python3.5 来使用不同的版本。但是 Go 呢?

我们知道 Go 的安装目录是 GOROOT:

$ go env GOROOT
/usr/local/go

在 macOS 上通常只有一个安装的版本。是否可能在 macOS 上安装多个 Go 版本并轻松切换?

英文:

Is there any good way to switch go version in macos?

if using python, we can define python2, python2.7, python3, python3.5 to use different version. but what about go?

we know the go install directory is GOROOT:

$ go env GOROOT
/usr/local/go

in macOS there usually only one installed. is it possible to install multi go in macOS and switch easily?

答案1

得分: 3

一种简单的方法是使用版本管理器gvm来完成。

安装完成后(根据您的系统),您可以使用以下命令选择您的Go版本(例如,Go 1.16):

gvm install go1.16
gvm use go1.16 [--default]
英文:

One easy way to do this is with the version manager gvm.

After installation (depending on your system), you can select your Go version (e.g., Go 1.16) with:

gvm install go1.16
gvm use go1.16 [--default]

答案2

得分: 1

如果Go不是你工具集中唯一的语言,你也可以看看asdf及其Golang插件。我个人喜欢它可以管理不同工具的版本,而不是为每种语言/工具安装单独的版本管理器。

asdf

Golang插件

英文:

If Go is not the only language in your toolset, you can also take a look at asdf and its Golang plugin. I personally like that it allows managing versions of different tools instead of installing a separate version manager for each language/tool.

答案3

得分: 1

你可以使用计算机上安装的默认go版本,当你需要使用其他版本时,可以使用docker中的go。我已经开发了这个函数,可以追加到~/.bashrc或~/.zshrc文件中:

# 允许你在计算机上不安装go 1.17的情况下使用它
# 使用示例:
# /your/go/project/directory - $ golang run main.go
# /your/go/project/directory - $ golang test ./... -p 1 -count 1
# /your/go/project/directory - $ golang build .
golang() {
  docker run --rm -v $PWD:/usr/src/myapp -w /usr/src/myapp golang:1.17 go "$@"
}

所以,假设你的电脑上有go 1.16,你可以这样使用go 1.17,而无需安装它:

golang run main.go
英文:

You can use a default go version installed in your computer and when you have to use other version you can use go in docker.
I've developed this function to be appended into ~/.bashrc or ~/.zshrc files:

# allows you to use go 17 without installing on your computer
# usage example: 
# /your/go/project/directory - $ golang run main.go
# /your/go/project/directory - $ golang test ./... -p 1 -count 1
# /your/go/project/directory - $ golang build .
golang() {
  docker run --rm -v $PWD:/usr/src/myapp -w /usr/src/myapp golang:1.17 go "$@"
}

So, let's say you have go 1.16 on your PC, you can use go 1.17 without even installing it, this way:

golang run main.go

huangapple
  • 本文由 发表于 2021年10月6日 19:47:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/69465140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定