Before upgrading Go to specific version, you need to upgrade the environment. Next refer to the following sections to upgrade Go version:
Upgrade the Go version in go.mod
The go.mod file specifices the Go version used in project.
Find the current Go version on the file line of go.mod file. For example, if the version is 1.19
, change to 1.22
(or whatever version you want to upgrade to). This step is curcial as it tells Go to compile your project with the specified version.
go 1.22
Update porject dependencies
Update your project’s dependencies in the terminal by following command:
Here, you only need to update the Go version in the go.mod file without updating any dependencies. After editing the go.mod file, you can run go mod tidy
to update your go.sum file.
go mod tidy
Please note that using
go get -u
can also update the version, but it will also update all dependencies to the latest version, including direct and indirect dependencies. This may cause certain parts of your project to no longer function properly, as the new versions of dependencies may behave differently from the old versions.
Now, your Go project should be upgraded to the latest Go version. You can use the go build or go run commands to check for any compilation errors. If there are any, you may need to fix these errors, or update your code to adapt to the new version of the Go language and dependencies.
In case you encounter any issues during the upgrade process, refer to the Go documentation or the documentation of the problematic dependency for troubleshooting tips.
After successfully upgrading your Go project, consider exploring the new features and improvements in the latest Go version to take full advantage of them in your project.
compile: version “go1.xx.xx” does not match go tool version “go1.xx.xx”
If you run go run main.go
and error happened (like fellowing)
compile: version "go1.19.4" does not match go tool version "go1.22.1"
Maybe have mutiple versions go installed in local
which -a go
and output like:
/Users/username/.asdf/shims/go
/usr/local/go/bin/go
If you are using asdf
, it mean you should uninstall following version:
sudo rm -rf /usr/local/go
And next should update the GOTOOT to asdf path like:
export GOROOT="$HOME/.asdf/installs/golang/{version}/go"