What are the implications of using the "go" version directive within a go module file (go.mod)
Asked Answered
A

1

12

Given the following go.mod file:

module foo

go 1.12

require (
    github.com/bar/baz v1.0.0
    github.com/rat/cat v1.0.0
)

What does the go 1.12 indicate? Does it prevent compiling the foo module against any other version of Go? Or is it simply an indicator of the foo's recommended/required Go version? Is this a directive that we should update whenever a new version of go is released (every 6 months)?

Aframe answered 2/10, 2019 at 15:25 Comment(2)
See golang.org/doc/go1.12#modulesProteinase
Also: golang.org/cmd/go/#hdr-The_go_mod_file: The expected language version, set by the go directive, determines which language features are available when compiling the module. Language features available in that version will be available for use. Language features removed in earlier versions, or added in later versions, will not be available. Note that the language version does not affect build tags, which are determined by the Go release being used.Rudderhead
M
12

It should be considered along the lines of a minimum required Go Version. If you build with the same or a higher version of Go, all should be fine as promised by the Go 1 compatibility promise. If you build with a lower version there will be an error message if the build fails:

The go directive in a go.mod file now indicates the version of the language used by the files within that module. It will be set to the current release (go 1.12) if no existing version is present. If the go directive for a module specifies a version newer than the toolchain in use, the go command will attempt to build the packages regardless, and will note the mismatch only if that build fails. Go 1.12 Release Notes

Mariammarian answered 2/10, 2019 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.