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

Copyfor 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.

Copyfor 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.

🌐
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
Discussions

loops - Is there a way to iterate over a range of integers? - Stack Overflow
P.S. The very first day in GoLang. Please, do critique if it's a wrong approach. ... Save this answer. ... Show activity on this post. Here's a benchmark to compare a Go for statement with a ForClause and a Go range statement using the iter package. ... Copypackage main import ( "testing" "github.com/bradfitz/iter" ) const loops = 1e6 ... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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.
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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).
🌐
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.
🌐
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) } }
🌐
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:
🌐
TutorialKart
tutorialkart.com › golang-tutorial › golang-range
Golang 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]) } }
🌐
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
🌐
Go
go.dev › tour › moretypes › 16
Range
Where to Go from here... The range form of the for loop iterates over a slice or map.
🌐
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.
🌐
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.
🌐
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   golang
🌐
The New Stack
thenewstack.io › home › how golang range simplifies data structure iteration
How Golang Range Simplifies Data Structure Iteration - The New Stack
May 31, 2024 - Those strings are “Ubuntu”, “Fedora”, “Pop!_OS”, “Linux Mint”, “elementaryOS” and “openSUSE”. In our for loop, we initialize the variable i to 0, make sure that i is less than the length of the distros array, and increment i by 1 at the end of each iteration. ... As you can see, using the range keyword simplifies and cleans up the code.