Change Go Version in Mod

Picking up an old Go project, we wanted to update the desired version it should run on (mainly so when we deployed to our live systems it would use this version too). To do this, we updated the version within the go.mod file - the file which keeps track of both versions and packages used by the project.

We used the command below:

1
go mod edit -go=1.19

This then means the go.mod file is updated to look like this:

1
2
3
4
5
6
7
module myproject

go 1.19

require (
    ... your packages ...
)

Heroku

To further this, if you’re deploying to Heroku, you can add a specific directive to the go.mod to set the Go version.

So it becomes this:

1
2
3
4
5
6
7
8
module myproject

// +heroku goVersion go1.19
go 1.19

require (
    ... your packages ...
)