Three new books, Go Optimizations 101, Go Details & Tips 101 and Go Generics 101 are published now. It is most cost-effective to buy all of them through this book bundle in the Leanpub book store.

The //go:build go1.mn comment directives don't work with Go toolchain v1.22.x versions when no go.mod files are involved

Go 1.22 changed the semantics of 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.

While for-range loops are almost unaffected, traditional for;; loops exhibit unexpected behaviors due to the breakage.

To keep using the old semantics prior to Go 1.22 in Go 1.22+ age, we must specify a Go version for each Go source file. One way to achieve this is adding a //go:build go1.21 comment directive at the beginning of the Go source file containing the old code.

While officially documented, due to a known bug, this way is currently non-functional with Go toolchain 1.22.x versions.

Here, I simply describe this bug: for a Go source file which Go version is not specified in a go.mod file, the //go:build go1.mn comment directive in it fails to specify the Go verison for it.

Here is an example:

//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
}

When run the Go file as a script with the 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 core team deliberately refused to fix the bug for coming 1.22.x versions. So you should be aware of the fact that the bug will be always in 1.22.x versions when you are using these Go toolchain versions.

(GoTV is a tool used to manage and use multiple coexisting installations of official Go toolchain versions harmoniously and conveniently.)


Index↡

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.

Tapir, the author of Go 101, has been on writing the Go 101 series books and maintaining the go101.org website since 2016 July. New contents will be continually added to the book and the website from time to time. Tapir is also an indie game developer. You can also support Go 101 by playing Tapir's games (made for both Android and iPhone/iPad):
Individual donations via PayPal are also welcome.

Index: