简体   繁体   English

如果 `go.mod` 中的版本低于实际版本,`go build` 是否会失败?

[英]Should `go build` fail if version in `go.mod` is lower than actual?

I have go 1.15 specified in the go.mod file.我在go.mod文件中指定了go 1.15

The current compiler version is the following:当前的编译器版本如下:

➜ go version
go version go1.14.4 linux/amd64

Yet, go build still successfully builds a binary.然而, go build仍然成功地构建了一个二进制文件。

Is this an expected behaviour?这是预期的行为吗? Not sure I can find explanation in the modules description.不确定我可以在模块描述中找到解释。

Yes, this is an expected behaviour.是的,这是预期的行为。

The go.mod is more of a tool for dependencies and package management than a tool for managing the output of the build process. go.mod 更多的是用于依赖关系和 package 管理的工具,而不是用于管理构建过程的 output 的工具。 That's why, if you're not using any features from 1.15, it should still be able to be built.这就是为什么,如果您不使用 1.15 中的任何功能,它仍然应该能够被构建。

The funny part is that if you try to build a project set to a version higher than the one that you're using (ie 1.15 built with go tool 1.14) and you mark in your go.mod that it needs to use the higher version, it'll show up in the build process:有趣的是,如果您尝试将项目设置为高于您正在使用的版本(即使用 go 工具 1.14 构建的 1.15)并且您在go.mod中标记它需要使用更高版本,它将显示在构建过程中:

# temp/test-go-build
./main.go:10:15: o.Redacted undefined (type url.URL has no field or method Redacted)
note: module requires Go 1.15

So the go tool tries it's best to fit your needs but won't block you from getting where you want to go (get it?).因此,go 工具会尽力满足您的需求,但不会阻止您到达 go(明白吗?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM