From Go 1.22 (expected release February 2024), you will be able to write:

for i := range 10 {
    fmt.Println(i+1)
}

(ranging over an integer in Go iterates from 0 to one less than that integer).

For versions of Go before 1.22, the idiomatic approach is to write a for loop like this.

for i := 1; i <= 10; i++ {
    fmt.Println(i)
}
Answer from Paul Hankin on Stack Overflow
🌐
Boldly Go
boldlygo.tech › archive › 2024-07-03-iteration-over-integers
Iteration over integers - Boldly Go
July 3, 2024 - I’m back from my holiday! I thought I’d send at least a few dalies over the last week while I was on vacation, but it wasn’t meant to be. But now that I’m back, we’ll pick up where we left off… For statements with range clause … For an integer value n, the iteration values 0 through n-1 are produced in increasing order.
Discussions

iterating over an integer?
what are the possible uses of this construct? Analysis of the public Go code corpus has revealed that it's by far the most common use case of the 3-clause for loop. More on reddit.com
🌐 r/golang
8
6
December 27, 2023
language lawyer - How to iterate an integer in for-range loop in Go 1.22? - Stack Overflow
Go 1.22 has been released today. According to the release note, "For" loops may now range over integers. For example: package main import "fmt" func main() { for i := ran... More on stackoverflow.com
🌐 stackoverflow.com
Preview: ranging over functions in Go
“Magic! We just iterate over…” The lack of magic in go is possibly it’s best feature. More on reddit.com
🌐 r/golang
109
July 22, 2023
Iterate over Integers
The Range syntax is still really obscure to me. I’m developing a simple image gallery. I have 10 images in the static folder and I want to write something like this for 10 times changing the image name with an index. I come from middleman, where i would write something like this 1..10.each ... More on discourse.gohugo.io
🌐 discourse.gohugo.io
0
1
October 24, 2017
🌐
Reddit
reddit.com › r/golang › iterating over an integer?
r/golang on Reddit: iterating over an integer?
December 27, 2023 -

so there's an upcoming feature where you can do for x := range n where n is an integer value. is that an attempt at avoiding a range syntax like Swift, Rust, and others have (m..n or m..=n or [m..n] or m...n) while at the same time having some thing that may in some sense resemble it? what are the possible uses of this construct? what is the rationale behind adding this to the language?

EDIT: what I find weird is that int is a scalar type in Golang, as I understand it, so how can you iterate over it. I mean semantically you cannot. but this is just simple syntactic sugar. I now get that this is a shorthand for one particular and popular case of C-derived for loop scheme where you routinely type out for i := 0; i < n; i++. so you can now just say for i := range n instead. no biggie. it's a very small thing to me.

if this saves someone a search, cool.

🌐
Google Groups
groups.google.com › g › golang-nuts › c › 7J8FY07dkW0
Range over int
Ha yes, I know I resurrected an old thread but this is high in the google results. The updated syntax in go 1.4 is still doesn't allow you to use a single integer for the range. ... -- You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
🌐
CodeSignal
codesignal.com › learn › courses › iterations-and-loops-in-go › lessons › mastering-loops-in-go-exploring-for-and-range
Mastering Loops in Go: Exploring `for` and `range`
Here, we define an int variable i, assign 1 to it first, then repeatedly execute fmt.Println(i) while i is less than or equal to 5, incrementing i by 1 after every iteration. The operation i++ simply adds 1 to the current value of i, and this operation is known as increment. ... In Go, the range clause is used to loop through items in a data structure such as an array, slice, string, map, or channel.
🌐
Medium
medium.com › @caring_smitten_gerbil_914 › understanding-range-loops-in-go-the-subtle-pitfalls-of-value-copying-c88ccfbba41c
🌀 Understanding Range Loops in Go: The Subtle Pitfalls of Value Copying | by Basant C. | Medium
June 30, 2025 - In for _, a := range accounts, each a is a copied struct. So modifying it changes nothing in the original slice. ... If you want to update all elements: the for i := range loop is shorter and idiomatic.
Find elsewhere
🌐
Go by Example
gobyexample.com › range
Go by Example: Range
range iterates over elements in a variety of data structures. Let’s see how to use range with some of the data structures we’ve already learned · Here we use range to sum the numbers in a slice. Arrays work like this too
🌐
Golang.cafe
golang.cafe › blog › how-to-iterate-over-range-int-golang
How To Iterate Over A Range Of Integers In Go (Golang) | Golang.cafe
April 27, 2022 - We know the basics and how easy is to use the for loop in Go (Golang), let’s see how we can iterate over a range of integers in Go, for example from 1 to 9 · for i:=0; i < 10; i++ { fmt.Println(i) } Like you would do in any other languages, iterating over a range of integers is straightforward in Go.
🌐
Reddit
reddit.com › r/golang › preview: ranging over functions in go
r/golang on Reddit: Preview: ranging over functions in Go
July 22, 2023 - You need to judge the RHS. this is not different than when you see A:= container.doSomething() Here you surely are not trying to judge the assignment operator but will need to know what doSomething() does. The same is with the range ... Except with the current proposal on the RHS there won't be a function call, but rather just an expression, and now I can't trust anything being ranged over ... Range over int should take a shorthand like start:stop:inc. It should also have a variable assignment (unless I missed that). Like for i := range 0:10:2 to range all evens to i.
🌐
HUGO
discourse.gohugo.io › t › iterate-over-integers › 8910
Iterate over Integers - HUGO
October 24, 2017 - The Range syntax is still really obscure to me. I’m developing a simple image gallery. I have 10 images in the static folder and I want to write something like this for 10 times changing the image name with an index. I come from middleman, where i would write something like this 1..10.each ...
🌐
Snakify
snakify.org › for loop with range
For loop with range - Learn Python 3 - Snakify
There's a reduced form of range() - range(max_value), in which case min_value is implicitly set to zero: ... Same as with if-else, indentation is what specifies which instructions are controlled by for and which aren't. Range() can define an empty sequence, like range(-5) or range(7, 3). In this case the for-block won't be executed: ... Let's have more complex example and sum the integers from 1 to n inclusively.
🌐
Exercism
exercism.org › tracks › go › concepts › range-iteration
Range Iteration in Go on Exercism
If you want to only print the index, you can replace the x with _, or simply omit the declaration: xi := []int{10, 20, 30} // for i, _ := range xi { for i := range xi { fmt.Println(i) } // outputs: // 0 // 1 // 2
🌐
JetBrains
youtrack.jetbrains.com › issue › GO-16398 › support-range-over-integer-for-go-1.22
support range over integer for go 1.22 : GO-16398
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
HUGO
discourse.gohugo.io › support
How to range every n items at a time - support - HUGO
March 11, 2018 - Hi! I was wondering if it’s possible to add 1 to the range index, so next iteration starts skipping one element of the list. Let me explain myself, I want to loop through my pages and display in a css column two of them, after another 2 in the next column. In order to do this I’m trying : {{ range $i := .Paginator.Pages }} {{ .Title }}{{ if .Draft }} ::Draft{{ end }} ...
🌐
Juejin
juejin.cn › s › golang range 1 to 10
golang range 1 to 10
We cannot provide a description for this page right now
🌐
YourBasic
yourbasic.org › golang › for-loop-range-array-slice-map-channel
4 basic range loop (for-each) patterns · YourBasic Go
ch := make(chan int) go func() { ch <- 1 ch <- 2 ch <- 3 close(ch) }() for n := range ch { fmt.Println(n) } ... For a nil channel, the range loop blocks forever. Here are two traps that you want to avoid when using range loops:
🌐
AskGolang
askgolang.com › how-to-iterate-over-a-range-of-integers-in-golang
How to Iterate Over a Range of Integers in Golang
July 2, 2023 - package main import ( "fmt" ) func main() { for i := range [10]int{} { fmt.Println(i) } } Output · 0 1 2 3 4 5 6 7 8 9 · That’s it. Krunal Lathiya · Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang.