Iterating Over a Range of Integers in Go
In Go, the built-in range keyword allows you to iterate over maps and slices. However, you may encounter scenarios where you need to iterate over a range of numbers.
Go 1.22 (February 2024)
Starting from Go 1.22, you can now easily iterate over a range of integers using the range syntax:
This will iterate from 0 (inclusive) to one less than the specified integer (9 in this case).
Go Versions Prior to 1.22
For versions of Go prior to 1.22, the idiomatic approach is to use a traditional for loop:
Ruby-Style Ranges
Go does not have a native implementation similar to Ruby's Range class to represent a range of integers.
The above is the detailed content of How to Iterate Over a Range of Integers in Go?. For more information, please follow other related articles on the PHP Chinese website!