英文:
Is there a way to make Golang compilation fail if the compiler is only a point-release too old?
问题
具体来说,对于我们的下一个软件发布,我想确保捕捉到在go 1.5.2中发布的一个错误修复;是否有办法在我们的构建服务器尝试使用Go 1.5.1或更早版本构建我的代码时使构建失败?
我知道构建约束(Build Constraints),我可以看到如何添加一个构建约束"go1.5"来确保使用"1.5或更高版本"的编译器,但是"go1.5.2"不起作用(似乎未定义构建标签go1.5.1和go1.5.2)。
另外,我也找不到一种方法来输出适用于构建的构建标签,然而这似乎是一件非常有用的事情。
英文:
Specifically, for our next software release, I want to make sure to catch a bug fix that was released in go 1.5.2; is there a way to make the build fail if our build server tries to build my code using Go 1.5.1 or earlier?
I know about Build Constraints, and I can see how I can add a build constraint of "go1.5" to make sure the "1.5 or greater" compiler is used, but "go1.5.2" doesn't work (it appears that build tags go1.5.1 and go1.5.2 are not defined.)
On a related note, I also can't find a way to dump out the build tags that apply for a build, and yet this seems to be a pretty useful thing to do.
答案1
得分: 1
你可以使用-ldflags
参数来传递配置的最小Go语言版本,并在init()
函数中检查运行时是否与指定的版本匹配。
package main
import "runtime"
// go run -ldflags "-X main.minGoVersion=go1.5.1" main.go
// 参考自 http://stackoverflow.com/questions/18409373/how-to-compare-two-version-number-strings-in-golang
func VersionOrdinal(version string) string {
// ISO/IEC 14651:2011
const maxByte = 1<<8 - 1
vo := make([]byte, 0, len(version)+8)
j := -1
for i := 0; i < len(version); i++ {
b := version[i]
if '0' > b || b > '9' {
vo = append(vo, b)
j = -1
continue
}
if j == -1 {
vo = append(vo, 0x00)
j = len(vo) - 1
}
if vo[j] == 1 && vo[j+1] == '0' {
vo[j+1] = b
continue
}
if vo[j]+1 > maxByte {
panic("VersionOrdinal: invalid version")
}
vo = append(vo, b)
vo[j]++
}
return string(vo)
}
var minGoVersion string
func init() {
if minGoVersion == "" {
panic("请传递 -ldflags \"-X main.minGoVersion=<version string>\" 参数")
}
current := VersionOrdinal(runtime.Version())
desired := VersionOrdinal(minGoVersion)
if current < desired {
panic("不支持的Go语言运行时版本 " + current + " < " + desired)
}
}
func main() {
}
英文:
You can use the -ldflags
to pass the configured min golang build and check at init() time if the runtime matches the specified version.
package main
import "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-golang
func VersionOrdinal(version string) string {
// ISO/IEC 14651:2011
const maxByte = 1<<8 - 1
vo := make([]byte, 0, len(version)+8)
j := -1
for i := 0; i < len(version); i++ {
b := version[i]
if '0' > b || b > '9' {
vo = append(vo, b)
j = -1
continue
}
if j == -1 {
vo = append(vo, 0x00)
j = len(vo) - 1
}
if vo[j] == 1 && vo[j+1] == '0' {
vo[j+1] = b
continue
}
if vo[j]+1 > maxByte {
panic("VersionOrdinal: invalid version")
}
vo = append(vo, b)
vo[j]++
}
return string(vo)
}
var minGoVersion string
func init() {
if minGoVersion == "" {
panic("please pass -ldflags \"-X main.minGoVersion=<version string> flag\"")
}
current := VersionOrdinal(runtime.Version())
desired := VersionOrdinal(minGoVersion)
if current < desired {
panic("unsupported golang runtime " + current + " < " + desired)
}
}
func main() {
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论