How to try out the new range func CL in a module

412 views
Skip to first unread message

Hein Meling

unread,
Aug 18, 2023, 4:54:23 PM8/18/23
to golang-nuts
Hi all,

I wanted to play around with the new range func CL.

Doing simple stuff works just fine, but if I need to import packages to construct my custom iterator func, the go/gotip command insists on a go.mod file, which effectively resets the go version to 1.21.0 (due to go.mod), instead of the "(w/ rangefunc)" CL.

Anyone know any workarounds for this?

Thanks,
:) Hein

Axel Wagner

unread,
Aug 21, 2023, 1:59:20 AM8/21/23
to Hein Meling, golang-nuts
Hm. For me, it still enables the rangefunc experiment, even though go.mod says go 1.21:

mero@vetinari ~/tmp/x$ gotip version
go version devel go1.21-ca691a8566d Tue Jul 18 10:30:20 2023 -0400 (w/ rangefunc) linux/amd64
mero@vetinari ~/tmp/x$ cat go.mod
module x

go 1.21
mero@vetinari ~/tmp/x$ cat x.go
package main

import "fmt"

func main() {
    s := []int{1, 2, 3}
    for v := range All(s) {
        fmt.Println(v)
    }
}

func All[T any](s []T) func(yield func(T) bool) bool {
    return func(yield func(T) bool) bool {
        for _, v := range s {
            if !yield(v) {
                return false
            }
        }
        return true
    }
}
mero@vetinari ~/tmp/x$ gotip run x.go
1
2
3

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f01ff376-b789-4d8a-89f5-165a6527325fn%40googlegroups.com.

Hein Meling

unread,
Aug 21, 2023, 6:50:58 PM8/21/23
to golang-nuts
Thanks Axel for your reply. That's interesting. I'm able to run your example just fine. However, using a different go.mod file with the content below (and a longer code example with iteration over a LevelDB thing... not able to provide the code), I get:

% gotip version
go version go1.21.0 darwin/arm64

% gotip run iter.go
# command-line-arguments
./iter.go:48:25: cannot range over SnapshotHash(ldb, nonce) (value of type Seq2[uint64, error])

module iter

go 1.21.0

require (
github.com/opencoff/go-fasthash v0.0.0-20180406145558-aed761496075
github.com/syndtr/goleveldb v1.0.0
)

require (
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
)



Axel Wagner

unread,
Aug 22, 2023, 1:43:45 AM8/22/23
to Hein Meling, golang-nuts
Your `gotip` version seems to be at Go 1.21.0, not at the `rangefunc` experiment CL. `gotip version` should say `go version devel go1.21-ca691a8566 Tue Jul 18 10:30:20 2023 -0400 (w/ rangefunc) linux/amd64` - regardless of the contents of the `go.mod`, as far as I know. At least I can't reproduce it saying anything different by tinkering with mine.

Axel Wagner

unread,
Aug 22, 2023, 1:53:05 AM8/22/23
to Hein Meling, golang-nuts
By coincidence, I was just reading https://go.dev/blog/toolchain and realized I put `GOTOOLCHAIN=local` into my ~/.confiig/go/env a while ago, so as to not have the Go tool transparently download different versions. If I remove that, I can indeed reproduce the behavior you are seeing.
So, my recommendation for experimenting would be to run `go env -w GOTOOLCHAIN=local`.

On Tue, Aug 22, 2023 at 12:51 AM Hein Meling <hein....@gmail.com> wrote:

Axel Wagner

unread,
Aug 22, 2023, 1:54:15 AM8/22/23
to Hein Meling, golang-nuts
(or change the `go` directive to `Go 1.21` (no minor version) apparently)

Hein Meling

unread,
Aug 28, 2023, 12:26:55 AM8/28/23
to golang-nuts
Yep! Can confirm that setting GOTOOLCHAIN=local and changing the go directive in go.mod to 1.21 (without minor version) fixed the problem.

Thanks again Axel!

:) Hein

Reply all
Reply to author
Forward
0 new messages