92

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?

James Haskell
  • 1,545
  • 2
  • 16
  • 21
  • 9
    Edit the file by hand or use `go mod edit -go=1.14`. – Charlie Tumahai Mar 13 '20 at 18:17
  • Thanks! So it is just change "go 1.13" to "go 1.14"? That's all there is to it? No need to run go mod tidy or any updates? – James Haskell Mar 13 '20 at 18:19
  • 1
    If you can simply change the go version whenever you wish with no ramifications, what is it for? Why is it in the go.mod file? – James Haskell Mar 13 '20 at 18:29
  • 12
    It specifies a minimum Go version for the project. If the build fails, the error output will indicate that the module specifies a newer version that was used to try to build it. – Adrian Mar 13 '20 at 18:32
  • 2
    It doesn't do anything else, so the question is kind of moot. It has nothing to do with `go mod tidy` or any of the dependencies. – Adrian Mar 13 '20 at 18:33
  • Thanks Adrian... on digging further, I found this: https://github.com/golang/go/issues/30791. Apparently I just don't need to worry about it. – James Haskell Mar 13 '20 at 18:35

4 Answers4

82

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 primilary for scripts so making changes to go.mod can easily be automated.

Community
  • 1
  • 1
icza
  • 389,944
  • 63
  • 907
  • 827
17

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.

Ebite Zion
  • 340
  • 2
  • 10
12

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

Zombo
  • 1
  • 62
  • 391
  • 407
9

This is how I have done it

go mod edit -go 1.18
go mod tidy
Osman Chowdhury
  • 121
  • 1
  • 3