Iterating Over a Range of Integers in Go
Go's range keyword provides a convenient way to iterate over data structures such as maps and slices. However, it cannot be directly used to iterate over a range of integers. This has led to the question of whether such functionality exists in Go.
From Go 1.22, you will gain the ability to iterate over integers using the range keyword. For example, the following code will iterate over the range 1 to 10 (inclusive):
For Go versions before 1.22, the idiomatic approach is to use a traditional for loop:
The above is the detailed content of How Can I Iterate Over a Range of Integers in Go?. For more information, please follow other related articles on the PHP Chinese website!