There is nothing wrong with your original code, when you are doing os.Args[1:] you are creating a new slice which like any slice starts at index 0.
It's a matter of style (and performance) but you could also do this:

Copyfor index, arg := range os.Args {
    if index < 1 {
        continue
    }
    s += fmt.Sprintf("%d: %s", index, arg)
}
Answer from Franck Jeannin on Stack Overflow
🌐
Go
go.dev › tour › moretypes › 16
Range
Flow control statements: for, if, else, switch and defer ... Congratulations! More types: structs, slices, and maps. ... Congratulations! ... Congratulations! ... Congratulations! ... Where to Go from here... The range form of the for loop iterates over a slice or map.
🌐
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

for-range loop over slice in Go
You're probably looking for this sentence from the language spec: "The range expression x is evaluated once before beginning the loop" https://go.dev/ref/spec#For_statements More on reddit.com
🌐 r/golang
9
1
December 29, 2021
Go loop indices for range on slice - Stack Overflow
I have a fairly simple question, but can't find answer anywhere. I'm iterating over a range of slices, like this: More on stackoverflow.com
🌐 stackoverflow.com
Range over slice (element vs index) - Code Review - Go Forum
Hello all, I’m pretty new to Go, just started learning it 6 weeks ago. I’m coming from using Python for the last 3 years, and am 100% self-taught (my ADHD makes it exceptionally hard to take formal classes or read a book cover to cover. Anyways, I am building my first Go project for work ... More on forum.golangbridge.org
🌐 forum.golangbridge.org
0
February 14, 2020
Go: Can you use range with a slice but get references? (iteration) - Stack Overflow
Say I want to change a value for all objects in an array. I like the range syntax a lot more than just named for loops. So I tried: type Account struct { balance int } type AccountList []Accou... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Go
go.dev › wiki › Range
Go Wiki: Range Clauses - The Go Programming Language
A range clause provides a way to iterate over an array, slice, string, map, or channel. for k, v := range myMap { log.Printf("key=%v, value=%v", k, v) } for v := range myChannel { log.Printf("value=%v", v) } for i, v := range myArray { log.Printf("array value at [%d]=%v", i, v) }
🌐
Reddit
reddit.com › r/golang › for-range loop over slice in go
r/golang on Reddit: for-range loop over slice in Go
December 29, 2021 -

I'm relatively new to the language and would like to learn the nuances around for-loops over slice. In the sample below, even though I modify the range variable inside the loop, the loop only iterates over the original values - why is that? I do know that slices are a combination of pointer plus length but I cannot understand well enough yet the behavior of the loop evaluation.

func rangeTest(slice []int) (result []int) {
	result = []int{}
	for _, n := range slice {
		if n%3 == 0 {
			slice = append(slice, n*33)
		}
		result = append(result, n)
	}
	return result
}

I tried finding documentation on the topic without much success.

https://go.dev/play/p/jQ7p2X-UqTY.go

🌐
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.
🌐
Ardan Labs
ardanlabs.com › blog › 2013 › 09 › iterating-over-slices-in-go.html
Iterating Over Slices In Go
September 23, 2013 - Don’t get me wrong, its great ... to iterate over a slice to perform some work. In Go we use the keyword range within a for loop construct to iterate over a slice....
🌐
Boldly Go
boldlygo.tech › archive › 2023-11-23-range-of-slice-expressions
Range of slice expressions - Boldly Go
Simple slice expressions … For arrays or strings, the indices are in range if 0
Find elsewhere
🌐
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) } }
🌐
Go Forum
forum.golangbridge.org › getting help › code review
Range over slice (element vs index) - Code Review - Go Forum
February 14, 2020 - Hello all, I’m pretty new to Go, just started learning it 6 weeks ago. I’m coming from using Python for the last 3 years, and am 100% self-taught (my ADHD makes it exceptionally hard to take formal classes or read a book cover to cover. Anyways, I am building my first Go project for work and have come across what may be a fairly trivial issue, but I haven’t been able to find a post on this specific question.
🌐
Calhoun
calhoun.io › does-range-copy-the-slice-in-go
Does Go's range Copy a Slice Before Iterating Over It? - Calhoun.io
You can read more about this here, and the code for a slice is shown below. type slice struct { array unsafe.Pointer len int cap int } This is important because it means that we can alter items in our slice, even when using the range keyword, and we might see those changes as we iterate over the slice.
🌐
Go
go.dev › blog › range-functions
Range Over Function Types - The Go Programming Language
August 20, 2024 - Here are the new functions in the slices package. All and Values are functions that return iterators over the elements of a slice. Collect fetches the values out of an iterator and returns a slice holding those values. See the docs for the others.
🌐
IncludeHelp
includehelp.com › golang › go-slice-ranges.aspx
Go Slice Ranges
May 5, 2025 - The basic slice range syntax involves specifying a start and an end index. You can create a new slice by slicing an existing slice within a specified range of indices. In this example, we create a new slice by specifying the start and end indices for the range.
🌐
Freshman Tech
freshman.tech › snippets › go › iterating-over-slices
How to iterate over slices in Go - Freshman.tech
May 22, 2021 - Iterating over a Go slice is greatly simplified by using a for..range loop:
🌐
Go
go.dev › tour › moretypes › 7
A Tour of Go
A slice is formed by specifying two indices, a low and high bound, separated by a colon: ... This selects a half-open range which includes the first element, but excludes the last one.
🌐
Coding Explorations
codingexplorations.com › blog › golang-slice-iteration-techniques-from-basic-to-advanced
Golang Slice Iteration Techniques: From Basic to Advanced — Coding Explorations
October 11, 2023 - package main import "fmt" func main() { fruits := []string{"apple", "banana", "cherry"} for _, fruit := range fruits { fmt.Println(fruit) } } Here, we use _ to discard the index, as we're only interested in the value. If you're working with a 2D array or slice, you can nest loops to iterate over each dimension.
🌐
GeeksforGeeks
geeksforgeeks.org › range-keyword-in-golang
Range Keyword in Golang - GeeksforGeeks
April 28, 2025 - Lets see what range returns while iterating over different kind of collections in Golang. Array or slice: The first value returned in case of array or slice is index and the second value is element.
🌐
Scaler
scaler.com › home › topics › golang › range loop in golang
Range Loop in Golang - Scaler Topics
May 4, 2023 - A for-range loop in Golang is used for iterating over elements in various data structures like an array, slice, map, or even a string, etc.
🌐
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 - In the above code, we’ve initialized a slice named “distros” as a list of strings. 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.