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

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

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()函数中检查运行时是否与指定的版本匹配。

  1. package main
  2. import "runtime"
  3. // go run -ldflags "-X main.minGoVersion=go1.5.1" main.go
  4. // 参考自 http://stackoverflow.com/questions/18409373/how-to-compare-two-version-number-strings-in-golang
  5. func VersionOrdinal(version string) string {
  6. // ISO/IEC 14651:2011
  7. const maxByte = 1<<8 - 1
  8. vo := make([]byte, 0, len(version)+8)
  9. j := -1
  10. for i := 0; i < len(version); i++ {
  11. b := version[i]
  12. if '0' > b || b > '9' {
  13. vo = append(vo, b)
  14. j = -1
  15. continue
  16. }
  17. if j == -1 {
  18. vo = append(vo, 0x00)
  19. j = len(vo) - 1
  20. }
  21. if vo[j] == 1 && vo[j+1] == '0' {
  22. vo[j+1] = b
  23. continue
  24. }
  25. if vo[j]+1 > maxByte {
  26. panic("VersionOrdinal: invalid version")
  27. }
  28. vo = append(vo, b)
  29. vo[j]++
  30. }
  31. return string(vo)
  32. }
  33. var minGoVersion string
  34. func init() {
  35. if minGoVersion == "" {
  36. panic("请传递 -ldflags \"-X main.minGoVersion=<version string>\" 参数")
  37. }
  38. current := VersionOrdinal(runtime.Version())
  39. desired := VersionOrdinal(minGoVersion)
  40. if current < desired {
  41. panic("不支持的Go语言运行时版本 " + current + " < " + desired)
  42. }
  43. }
  44. func main() {
  45. }
英文:

You can use the -ldflags to pass the configured min golang build and check at init() time if the runtime matches the specified version.

  1. package main
  2. import &quot;runtime&quot;
  3. // go run -ldflags &quot;-X main.minGoVersion=go1.5.1&quot; main.go
  4. // from http://stackoverflow.com/questions/18409373/how-to-compare-two-version-number-strings-in-golang
  5. func VersionOrdinal(version string) string {
  6. // ISO/IEC 14651:2011
  7. const maxByte = 1&lt;&lt;8 - 1
  8. vo := make([]byte, 0, len(version)+8)
  9. j := -1
  10. for i := 0; i &lt; len(version); i++ {
  11. b := version[i]
  12. if &#39;0&#39; &gt; b || b &gt; &#39;9&#39; {
  13. vo = append(vo, b)
  14. j = -1
  15. continue
  16. }
  17. if j == -1 {
  18. vo = append(vo, 0x00)
  19. j = len(vo) - 1
  20. }
  21. if vo[j] == 1 &amp;&amp; vo[j+1] == &#39;0&#39; {
  22. vo[j+1] = b
  23. continue
  24. }
  25. if vo[j]+1 &gt; maxByte {
  26. panic(&quot;VersionOrdinal: invalid version&quot;)
  27. }
  28. vo = append(vo, b)
  29. vo[j]++
  30. }
  31. return string(vo)
  32. }
  33. var minGoVersion string
  34. func init() {
  35. if minGoVersion == &quot;&quot; {
  36. panic(&quot;please pass -ldflags \&quot;-X main.minGoVersion=&lt;version string&gt; flag\&quot;&quot;)
  37. }
  38. current := VersionOrdinal(runtime.Version())
  39. desired := VersionOrdinal(minGoVersion)
  40. if current &lt; desired {
  41. panic(&quot;unsupported golang runtime &quot; + current + &quot; &lt; &quot; + desired)
  42. }
  43. }
  44. func main() {
  45. }

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:

确定