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
🌐
Reddit
reddit.com › r/golang › slices i+1 in a range clause
r/golang on Reddit: Slices i+1 in a range clause
June 3, 2023 -

Hey all.

New programmer here.

I was wondering, when composing slices, specifically in a for/index/range situation, when it comes time to fmt.Println, is it bad practice to include a +1 in the index, so that when it prints it would do so starting at 1 as opposed to 0 for readability? Or is that blasphemous?

Thank you.

Discussions

how counting up a variable in range from 1 to n,isn't start 0
There was an error while loading. Please reload this page More on github.com
🌐 github.com
3
May 12, 2019
spec: add range over int, range over func
Following discussion on #56413, I propose to add two new types that a for-range statement can range over: integers and functions. In the spec, the table that begins the section would have a few mor... More on github.com
🌐 github.com
417
July 17, 2023
Slices i+1 in a range clause
In general, if the output is for technical purposes, use the actual index - programmers will think in zero-indexed terms anyway, and it will avoid confusion. If the output is for someone non-technical, then using 1 for the first element is fine. More on reddit.com
🌐 r/golang
12
0
June 3, 2023
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
🌐
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
🌐
DoltHub
dolthub.com › blog › 2024-07-12-golang-range-iters-demystified
Go range iterators demystified | DoltHub Blog
July 12, 2024 - There are three types of range iterator functions, one for each form of the range loop. So they take 0, 1, or 2 arguments. The one above takes 1 argument.
🌐
Scaler
scaler.com › home › topics › golang › range loop in golang
Range Loop in Golang - Scaler Topics
May 4, 2023 - Program to use the for-range loop on a string for accessing characters. ... Explanation: the for-range loop starts from the starting character which is H to the last character which is o and prints each of the character.
🌐
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.
🌐
Dot Net Perls
dotnetperls.com › for-go
Go - for Loop Examples - Dot Net Perls
With range, a keyword, we can iterate over more complex things like slices or maps. Range, used with for, helps us manage iteration. This code uses the classic for-loop where we begin at a certain number and end at another. After each body evaluation, the index is incremented (1 is added to it).
Find elsewhere
🌐
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.
🌐
Google Groups
groups.google.com › g › golang-nuts › c › 7J8FY07dkW0
Range over int
Looks nice visually. Since 'range' is used to mean "each element within" then you would need to believe that people will understand that [0..N-1] means "within" N -- that starting at 1 or ending at N are not to be expected.
🌐
Go
go.dev › wiki › Range
Go Wiki: Range Clauses - The Go Programming Language
items := make([]map[int]int, 10) for i := range items { items[i] = make(map[int]int, 1) items[i][1] = 2 } This content is part of the Go Wiki. go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.
🌐
GeeksforGeeks
geeksforgeeks.org › range-keyword-in-golang
Range Keyword in Golang - GeeksforGeeks
April 28, 2025 - Channel: The first value returned in channel is element and the second value is none. Now, let's see some examples to illustrate the usage of range keyword in Golang. Example 1:
🌐
TutorialsPoint
tutorialspoint.com › go › go_range.htm
Go - Range
The following paragraph shows how to use range − · package main import "fmt" func main() { /* create a slice */ numbers := []int{0,1,2,3,4,5,6,7,8} /* print the numbers */ for i:= range numbers { fmt.Println("Slice item",i,"is",numbers[i]) } /* create a map*/ countryCapitalMap := map[string] string {"France":"Paris","Italy":"Rome","Japan":"Tokyo"} /* print map using keys*/ for country := range countryCapitalMap { fmt.Println("Capital of",country,"is",countryCapitalMap[country]) } /* print map using key-value*/ for country,capital := range countryCapitalMap { fmt.Println("Capital of",country,"is",capital) } }
🌐
TutorialKart
tutorialkart.com › golang-tutorial › golang-range
Go Range
May 26, 2021 - package main import "fmt" func main() { var evens = [5] int {2, 4, 6, 8, 10} // using index and element returned from range for index,element:= range evens { fmt.Printf("evens[%d] = %d \n", index, element) } } ... In the following examples, we will take a String and iterate over the characters of it, using for loop and range. In this example, we will iterate over characters of a given string using range. ... package main import "fmt" func main() { var str = "Golang" // using index returned from range for index:= range str { fmt.Printf("str[%d] = %d \n", index, str[index]) } }
🌐
GitHub
github.com › golang › go › issues › 61405
spec: add range over int, range over func · Issue #61405 · golang/go
July 17, 2023 - Following discussion on #56413, I propose to add two new types that a for-range statement can range over: integers and functions. In the spec, the table that begins the section would have a few more rows added: Range expression 1st value...
Author   rsc
🌐
Learnerslesson
learnerslesson.com › GO › Go-For-Range-Loop.htm
GO -FOR RANGE LOOP
package main import ("fmt") func main() { x := "Hello" for i,letter := range x { fmt.Printf("Index : %d Value : %c \n", i, letter) } } Index : 0 Value : H Index : 1 Value : e Index : 2 Value : l Index : 3 Value : l Index : 4 Value : o
🌐
Programiz
programiz.com › golang › range
Go range (With Examples)
We use range with the for loop to iterate through the elements of array, string or map. In this tutorial, we will learn about Go for range with the help of examples.
🌐
Go by Example
gobyexample.com › range-over-iterators
Go by Example: Range over Iterators
Starting with version 1.23, Go has added support for iterators, which lets us range over pretty much anything · Let’s look at the List type from the previous example again. In that example we had an AllElements method that returned a slice of all elements in the list.
🌐
YourBasic
yourbasic.org › golang › for-loop-range-array-slice-map-channel
4 basic range loop (for-each) patterns · YourBasic Go
yourbasic.org/golang · Basic for-each loop (slice or array) String iteration: runes or bytes · Map iteration: keys and values · Channel iteration · Gotchas · a := []string{"Foo", "Bar"} for i, s := range a { fmt.Println(i, s) } 0 Foo 1 Bar · The range expression, a, is evaluated once before beginning the loop.