有没有办法让Golang编译在编译器版本过旧时失败?

huangapple go评论78阅读模式
英文:

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 &quot;runtime&quot;
// go run -ldflags &quot;-X main.minGoVersion=go1.5.1&quot; 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&lt;&lt;8 - 1
vo := make([]byte, 0, len(version)+8)
j := -1
for i := 0; i &lt; len(version); i++ {
b := version[i]
if &#39;0&#39; &gt; b || b &gt; &#39;9&#39; {
vo = append(vo, b)
j = -1
continue
}
if j == -1 {
vo = append(vo, 0x00)
j = len(vo) - 1
}
if vo[j] == 1 &amp;&amp; vo[j+1] == &#39;0&#39; {
vo[j+1] = b
continue
}
if vo[j]+1 &gt; maxByte {
panic(&quot;VersionOrdinal: invalid version&quot;)
}
vo = append(vo, b)
vo[j]++
}
return string(vo)
}
var minGoVersion string
func init() {
if minGoVersion == &quot;&quot; {
panic(&quot;please pass  -ldflags \&quot;-X main.minGoVersion=&lt;version string&gt; flag\&quot;&quot;)
}
current := VersionOrdinal(runtime.Version())
desired := VersionOrdinal(minGoVersion)
if current &lt; desired {
panic(&quot;unsupported golang runtime &quot; + current + &quot; &lt; &quot; + desired)
}
}
func main() {
}

huangapple
  • 本文由 发表于 2015年12月16日 05:17:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/34299556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定