If you would like to learn some Go details and facts every serveral days, please follow @zigo_101.
go 1.mn directive line in go.mod file doesn't work with some Go toolchain versions for some Go source files which contain //line ... comment directives
for-range and traditional for;; loops. Because 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, some old traditional for;; loops exhibit unexpected behaviors due to the breakage.
//go:build go1.mn comment at the very beginning of a Go source file. However, certain versions of the Go toolchain are known to have bugs when handling this directive.
go 1.mn directive in the go.mod file of the containing module of a Go source file. Unfortunately, some versions of the Go toolchain also contain bugs in how they process this.
go.mod file specifies version 1.21. If that module is compiled using Go toolchain 1.22.x or 1.23.y, the program will print 210 (the new behavior) instead of the expected 333 (the old behavior). This demonstrates that the toolchain is failing to respect the version specified in the go.mod file.
module main go 1.21
//line main.go:1
package main
func main() {
for i := 0; i < 3; i++ {
defer func() {
print(i)
}()
}
}
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.
go 1.mn directive line in go.mod file doesn't work with some Go toolchain versions for some Go source files which contain //line ... comment directives