Home >Backend Development >Golang >How Can I Iterate Over a Range of Integers in Go?

How Can I Iterate Over a Range of Integers in Go?

Susan Sarandon
Susan SarandonOriginal
2024-12-21 10:19:10224browse

How Can I Iterate Over a Range of Integers in Go?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn