1

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?

4
  • 5
    Did you check this page: golang.org/doc/manage-install Commented Oct 6, 2021 at 11:49
  • 1
    Any Go installation done "a usual way" is self-contained in that it's a single directory, and when you run {that_dir}/bin/go than entry-point program knows how to access all the other programs it calls—so that all that tools are of the correct version. I for one, merely have a local Git clone of the upstream repo and use git worktree add + go build to build the next upstream release with the previous one, so I naturally end up with multiple Go installations. One of them is made "main" by adding its bin directory to the contents of the PATH environment variable. Commented Oct 6, 2021 at 12:08
  • There is no general case for switching versions, since each release has been backwards compatible, and only the 2 most recent are supported. There are some community supported tools for controlling the versions, but you won't find them used much in practice (and that is off-topic here). Commented Oct 6, 2021 at 12:28
  • I want to contribute to a large opensource project, there should often swich go version for adapting to environment. Commented Oct 6, 2021 at 12:40

3 Answers 3

4

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] 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.