Skip to content

Instantly share code, notes, and snippets.

@achille-roussel
Last active January 29, 2024 22:50
Show Gist options
  • Save achille-roussel/5a9afe81c91891de4fad0bfe0965a9ea to your computer and use it in GitHub Desktop.
Save achille-roussel/5a9afe81c91891de4fad0bfe0965a9ea to your computer and use it in GitHub Desktop.
Go 1.22 rc1 installation to enable the range functions experiment

This file describes how to install Go 1.22 and enable the range functions experimental feature.

Go has standard instructions for managing multiple installations at https://go.dev/doc/manage-install, this document focuses on how to install Go 1.22 rc1 to enable the range functions experiment.

💡 These instructions are only useful until Go 1.22 is officially released (the target is February 2024)

💡 Go 1.22 rc2 has now been released, these instructions are still valid, simply replace rc1 with rc2 to adapt (e.g., https://pkg.go.dev/golang.org/dl/go1.22rc2)

Installing Go 1.22 rc1

Go provides a tool to download and install 1.22 rc1 at: https://pkg.go.dev/golang.org/dl/go1.22rc1

To summarize, these are the commands to run:

go install golang.org/dl/go1.22rc1@latest
go1.22rc1 download

These commands install the go1.22rc1 binary in $GOPATH/bin/go1.22rc1, and install the files in $HOME/sdk/go1.22rc1.

Note that you need an existing installation of Go to set it up, see https://go.dev/doc/install if needed.

If the GOPATH is in your shell PATH, you should be able to run the following command, which prints the version of the go program, along with the architecture and operating system that it was built for:

go1.22rc1 version

If GOPATH is not in your PATH, update the PATH environment variable accordingly:

export PATH="$GOPATH/bin:$PATH"

It is useful to have this expression in the ~/.bashrc or ~/.zshrc file so it gets set automatically when launching a shell.

Enabling the range functions experiment

Go uses an environment variable called GOEXPERIMENT to control which experimental features to enable. The name of the experimental feature for range functions is rangefunc, and therefore can be enabled with:

export GOEXPERIMENT=rangefunc

Once again, it is useful to add this export to a ~/.bashrc or ~/.zshrc file so all new shells have the environment variable set.

However, if we run a go command, we now get the following error:

$ go version
go: unknown GOEXPERIMENT rangefunc

This is because the go program isn't version 1.22 yet, we are still running the previous version of Go which did not accept rangefunc as a valid experiment.

To address this, we can instead directly use the go1.22rc1 program instead of go, but that only stretches so far since a lot of processes will likely expect the go command to be the way to invoke the toolchain, and will suffer from the same issue.

We can address this by setting a symbolic link for go to go1.22rc1 with the following command:

ln -s go1.22rc1 $(dirname $(which go1.22rc1))/go
$ go version
go version go1.22rc1 darwin/arm64

This works because the GOPATH appears before the location of the main Go installation in the PATH environment variable, so the symbolic link to go1.22rc1 is found first.

Disabling and uninstalling

To disable the range functions experimental feature:

unset GOEXPERIMENT

To revert to the main Go installation:

rm $(dirname $(which go1.22rc1))/go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment