如果编译器只是一个太旧的点发布,有没有办法让 Golang 编译失败?

具体来说,对于我们的下一个软件版本,我想确保捕获在 go 1.5.2 中发布的错误修复;如果我们的构建服务器尝试使用 Go 1.5.1 或更早版本构建我的代码,有没有办法使构建失败?

我知道构建约束,我可以看到如何添加“go1.5”的构建约束以确保使用“1.5 或更高”编译器,但“go1.5.2”不起作用(似乎未定义构建标记 go1.5.1 和 go1.5.2。)

在相关说明中,我也找不到一种方法来转储适用于构建的构建标签,但这似乎是一件非常有用的事情。


倚天杖
浏览 154回答 1
1回答

呼唤远方

您可以使用-ldflags传递配置的 min golang 构建并在 init() 时间检查运行时是否与指定版本匹配。package mainimport "runtime"// go run -ldflags "-X main.minGoVersion=go1.5.1" main.go// from http://stackoverflow.com/questions/18409373/how-to-compare-two-version-number-strings-in-golangfunc VersionOrdinal(version string) string {&nbsp; &nbsp; // ISO/IEC 14651:2011&nbsp; &nbsp; const maxByte = 1<<8 - 1&nbsp; &nbsp; vo := make([]byte, 0, len(version)+8)&nbsp; &nbsp; j := -1&nbsp; &nbsp; for i := 0; i < len(version); i++ {&nbsp; &nbsp; &nbsp; &nbsp; b := version[i]&nbsp; &nbsp; &nbsp; &nbsp; if '0' > b || b > '9' {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vo = append(vo, b)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if j == -1 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vo = append(vo, 0x00)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = len(vo) - 1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if vo[j] == 1 && vo[j+1] == '0' {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vo[j+1] = b&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if vo[j]+1 > maxByte {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic("VersionOrdinal: invalid version")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; vo = append(vo, b)&nbsp; &nbsp; &nbsp; &nbsp; vo[j]++&nbsp; &nbsp; }&nbsp; &nbsp; return string(vo)}var minGoVersion stringfunc init() {&nbsp; &nbsp; if minGoVersion == "" {&nbsp; &nbsp; &nbsp; &nbsp; panic("please pass&nbsp; -ldflags \"-X main.minGoVersion=<version string> flag\"")&nbsp; &nbsp; }&nbsp; &nbsp; current := VersionOrdinal(runtime.Version())&nbsp; &nbsp; desired := VersionOrdinal(minGoVersion)&nbsp; &nbsp; if current < desired {&nbsp; &nbsp; &nbsp; &nbsp; panic("unsupported golang runtime " + current + " < " + desired)&nbsp; &nbsp; }}func main() {}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go