Reputation: 2035
What is the proper way to upgrade the go version in a go mod, specifically 1.13 to 1.14?
Do you simply edit the go.mod file and change go 1.13
to go 1.14
?
I'm not asking about how to edit the go.mod file, I'm asking if it is proper to simply change the go version in the go.mod file, and everything else (all the dependencies) is still ok for the project?
Upvotes: 141
Views: 139093
Reputation: 418435
Command go: Edit go.mod from tools or scripts:
Usage:
go mod edit [editing flags] [go.mod]
Edit provides a command-line interface for editing go.mod, for use primarily by tools or scripts. It reads only go.mod; it does not look up information about the modules involved. By default, edit reads and writes the go.mod file of the main module, but a different target file can be specified after the editing flags.
...
The -go=version flag sets the expected Go language version.
So simply:
go mod edit -go=1.14
But you may also edit go.mod
manually, it's a simple text file. go mod edit
is primarily for scripts so making changes to go.mod
can easily be automated.
Upvotes: 118
Reputation: 9135
I you are working with go.work
you can do it with this script. It will change all child mod version also
#!/bin/bash
# Function to recursively upgrade Go modules
upgrade_go_modules() {
# Prompt user for Go version
read -p "Enter the Go version (e.g., 1.17): " go_version
if [ -z "$go_version" ]; then
echo "Go version not provided. Exiting."
exit 1
fi
# Update go.work file in the root directory
sed -i "s/go[[:space:]]\+[0-9]\+\(\.[0-9]\+\)*$/go $go_version/" go.work
# Upgrade Go modules
for mod_file in $(find . -name "go.mod"); do
dir=$(dirname "$mod_file")
echo "Updating Go version in $dir"
(cd "$dir" && go mod edit -go="$go_version")
done
}
# Main execution
upgrade_go_modules
Upvotes: 0
Reputation: 2087
In addition to the provided solutions for updating go.mod
file,
If your project is dockerized, don't forget to update the docker base image as well, e.g.:
FROM golang:1.17 AS build
to
FROM golang:1.20 AS build
Upvotes: 3
Reputation: 361
This is how I have done it
go mod edit -go 1.18
go mod tidy
Upvotes: 22
Reputation: 380
The answers supplied here helped me alot. But a little adjustment may be due especially for Windows users.
I used on the command prompt:
go mod edit -go 1.17
And not:
go mod edit -go=1.17
Note the omission of ''=" sign.
Upvotes: 21
Reputation: 1
The other answer is good, but as another method, say you have this:
module north
go 1.13
you can just delete the go
line, and run go mod tidy
. Result:
module north
go 1.16
https://golang.org/cmd/go#hdr-Add_missing_and_remove_unused_modules
Upvotes: 21