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
For range with ints - Technical Discussion - Go Forum
chatgpt is saying that for range with integer works different, like it is converted to an iterable type first, (to string) and iterates over rune ‘5’ only once, but when I run it in playground, it is working like I would have written: for i := 0; i More on forum.golangbridge.org
🌐 forum.golangbridge.org
0
November 27, 2024
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
🌐
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.
🌐
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.
Find elsewhere
🌐
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. Or you alternatively you could use the range construct and range over an initialised empty slice of integers · for i := range [10]int{} { fmt.Println(i) } Looking to hire talented Golang engineers? Connect with top developers and scale your team faster · Start Hiring
🌐
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.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › go language › range-keyword-in-golang
Range Keyword in Golang - GeeksforGeeks
July 12, 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:
🌐
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.
🌐
Go Forum
forum.golangbridge.org › technical discussion
For range with ints - Technical Discussion - Go Forum
November 27, 2024 - chatgpt is saying that for range with integer works different, like it is converted to an iterable type first, (to string) and iterates over rune ‘5’ only once, but when I run it in playground, it is working like I would have written: for i := 0; i
🌐
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]) } }
🌐
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) } }
🌐
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
🌐
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.
🌐
Go
go.dev › blog › range-functions
Range Over Function Types - The Go Programming Language
August 20, 2024 - However, Go before version 1.23 did not. As we all know, Go has container types that are built in to the language: slices, arrays, and maps. And it has a way to access the elements of those values without exposing the underlying representation: the for/range statement.