Iterating over integers in Go

February 19, 2024
Iterating over integers in Go

With the release of version 1.22, Go introduced native support for a new type of loop - the range-over integer. This simplifies the for-loop’s syntax in specific scenarios, though its capabilities are more restricted compared to range functions found in languages like Python or PHP. It permits iteration only from 0 to n, without support for a start value or step. In this post, we'll look into the new syntax with examples.

Range-over Integer Loop

The syntax for the range-over integer loop is straightforward:

    
  

Both are equivalent to the traditional for-loop:

    
  

Here are a couple of things to keep in mind:

  • The upper range is exclusive - the loop executes n-1 times.
  • n must be non-negative ( n > 0 ) for the loop to run.
  • Unlike other languages (looking at you Python), there’s no way to specify a starting value, step, or iterate backward.

What is the use case?

New features always seem cool and shiny, but they don't always live up to expectations. In this case, I genuinely feel that it has potential for success. However, the addition of this syntactic sugar for something as trivial as a for loop made me question its necessity - does it genuinely enhance simplicity, or does it risk oversimplification? I tried to find an answer to that by looking into where and how I could apply the new syntax right away and how big of a demand it was from the community.

For the first part, I quickly checked a couple of projects in search of standard for-loops. The results? I noticed that I don't use them too often; however, most occurrences can be substituted with the new syntax. For example, a benchmark can be transformed into:

    
  

To search your own projects for traditional for-loops, try using:

    
  

I also think that using the new syntax when iterating over slices with an index adds a bit of extra verbosity that I personally appreciate. Maybe, as the adoption of Go 1.22 grows, we’ll start seeing this kind of construct more often:

    
  

Go's philosophy revolves around simplicity. So, does the community want to streamline an already trivial task of writing a for loop?

Searching the Internet results in plenty of examples of workarounds that people used before native support. For example, a fixed-size array:

    
  

Or using a slice, exemplified by a dedicated function:

    
  

Arguably, this approach might even seem like a neat idea - it’s clean and does not result in any extra allocations. You can even find open-source libraries that do nothing else than that, as well as more feature-rich ones, which remind of Python’s range function.

One of the first results in Google for “range over integer Golang” shows an ongoing thread in Golang Nuts Google Group started on Jan 19, 2012 (!sick).

I like the new syntax, even if there’s not a lot of places to use it. Also, maybe it just made sense to add it, considering more changes to loops are headed to the language (range-over function). Share your thoughts! Happy coding! 💻

Go