如果編譯器只是一個太舊的點發(fā)布,有沒有辦法讓 Golang 編譯失?。?/h1>
1 回答

TA貢獻1856條經(jīng)驗 獲得超11個贊
您可以使用-ldflags傳遞配置的 min golang 構(gòu)建并在 init() 時間檢查運行時是否與指定版本匹配。
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() {
}
- 1 回答
- 0 關(guān)注
- 183 瀏覽
添加回答
舉報