mmcgrana / gobyexample

Go by Example
https://gobyexample.com
7.16k stars 1.26k forks source link

cannot range over 3 (untyped int constant)compilerInvalidRangeExpr #544

Closed kkchu791 closed 1 month ago

kkchu791 commented 1 month ago

In the For section, there's this code

    for i := range 3 {
        fmt.Println("range", i)
    }

It seems you can't iterate over an integer, you can only use range for array, slice, string, map, or channel.

eliben commented 1 month ago

https://github.com/mmcgrana/gobyexample?tab=readme-ov-file#what-version-of-go-is-required-to-run-these-examples

kkchu791 commented 1 month ago

Would it be best to just remove the examples with range <int>?

eliben commented 1 month ago

Would it be best to just remove the examples with range <int>?

This feature was released in Go 1.22, about 6 months ago. We expect gobyexample to be mostly used for folks who are learning the language, and these people don't have a reason to use old versions. Moreover, people turn to gobyexample specifically to find out how new features work in concrete small code examples. Therefore, we try to keep examples up to date with the newest language features.