I am using Visual Studio Code version 1.33.1 as IDE for our Go applications. We would like to use Go version 1.11 for our applications. However looks like one or more of the dependencies we are using has downloaded a package for Go 1.12. Now, VS Code is failing to build the application with the following error:
go build golang.org/x/sys/unix: module requires Go 1.12 go build github.com/pelletier/go-toml: module requires Go 1.12 go [1,1]
I tried re-installing Go 1.11, removed the offending packages and let it reinstall. No matter when I tried to build VS Code is downloading the 1.12 version fails to build. I would like VS Code not to download 1.12 version of the packages and restrict it to 1.11 only.
Go 1.12 Release Notes (February 2019)
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.
This changed use of the go directive means that if you use Go 1.12 to build a module, thus recording go 1.12 in the go.mod file, you will get an error when attempting to build the same module with Go 1.11 through Go 1.11.3. Go 1.11.4 or later will work fine, as will releases older than Go 1.11. If you must use Go 1.11 through 1.11.3, you can avoid the problem by setting the language version to 1.11, using the Go 1.12 go tool, via go mod edit -go=1.11.
$ go help go.mod The go.mod file itself is line-oriented, with // comments but no /* */ comments. Each line holds a single directive, made up of a verb followed by arguments. For example: module my/thing go 1.12 require other/thing v1.0.2 require new/thing/v2 v2.3.4 exclude old/thing v1.2.3 replace bad/thing v1.4.5 => good/thing v1.4.5 The verbs are module, to define the module path; go, to set the expected language version; require, to require a particular module at a given version or later; exclude, to exclude a particular module version from use; and replace, to replace a module version with a different module version.
A possible solution to your problem was first introduced in Go1.12: the go.mod
verb go
, to set the expected language version.
UPDATE:
Comment: I tried using the suggested command: go mod edit -go=1.11 I got an error: flag provided but not defined: -go I manually edited to add go 1.11 right under my module declaration in all go.mod files, it did not work. – user2995358
Your results are expected. As I explained before and the documentation states, the go.mod
verb go
was first introduced in Go1.12.
For example, the expected results,
$ go version
go version go1.11.10 linux/amd64
$ go mod edit -go=1.11
flag provided but not defined: -go
usage: go mod edit [editing flags] [go.mod]
Run 'go help mod edit' for details.
$
$ go version
go version go1.12.5 linux/amd64
$ go mod edit -go=1.11
$
Read the documentation:
$ go version go version go1.11.10 linux/amd64 $ go1.11 help modules Preliminary module support Go 1.11 includes preliminary support for Go modules, including a new module-aware 'go get' command. We intend to keep revising this support, while preserving compatibility, until it can be declared official (no longer preliminary), and then at a later point we may remove support for work in GOPATH and the old 'go get' command. $ $ go version go version devel +004fb5cb8d Fri May 3 03:49:11 2019 +0000 linux/amd64 $ go help modules Module support Go 1.13 includes official support for Go modules, including a module-aware 'go get' command. Module-aware mode is active by default. $
Go 1.11 includes only preliminary support for Go modules. Go 1.13 includes full official support for Go modules.
Why do you expect everything to work flawlessly in Go1.11 with only preliminary support?