//go:build go1.mn
comment directives don't work with Go toolchain v1.22.x versions when no go.mod
files are involved
for-range
and tranditional for;;
loops. Since the semantic changes alter the behavior of existing code written according to the previous semantics, they break backward compatibility.
for-range
loops are almost unaffected, traditional for;;
loops exhibit unexpected behaviors due to the breakage.
//go:build go1.21
comment directive at the beginning of the Go source file containing the old code.
go.mod
file, the //go:build go1.mn
comment directive in it fails to specify the Go verison for it.
//go:build go1.21
package main
import "fmt"
func filter(n int) bool {
return n&3 == 1
}
// Search values and return them without perversing order.
func search(start, end int) (r []int) {
var count = 0
for i, index := start, 0; i <= end; i++ {
if filter(i) {
count++
defer func(value int) {
r[index] = value
index++
}(i)
}
}
r = make([]int, count)
return
}
go run
command (without a go.mod
file involved), the example program prints [13 0]
with Go toolchain 1.22.x versions, due to Go 1.22+ semantics are used, which is wrongly. It should prints [17 13]
, as with older and newer toolchain versions.
$ gotv 1.21. run example.go [Run]: $HOME/.cache/gotv/tag_go1.21.12/bin/go run aa.go [17 13] $ gotv 1.22. run example.go [Run]: $HOME/.cache/gotv/tag_go1.22.5/bin/go run aa.go [13 0] $ gotv 1.23. run example.go [Run]: $HOME/.cache/gotv/tag_go1.23rc1/bin/go run aa.go [17 13]
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.