Golang iterate over array: range and for loops with len

Golang iterate over array and golang array loop with for range; golang for loop array and golang loop through array using len; go iterate over array and iterate over array golang with index or values only.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang iterate over array: range and for loops with len

In Go, [n]T is an array of n values of type T; for example var a [10]int is ten integers laid out in memory. Searches such as golang iterate over array, golang array loop, golang for loop array, go iterate over array, golang loop through array, golang iterate array, go loop through array, golang loop over array, iterate over array golang, and go loop over array all map to the same two patterns: a for with range, or a classic index loop with len. This tutorial shows both on [5]string and [5]int arrays. For general loop syntax, see the for loop in Go guide.

Tested with Go 1.24 on Linux.


Range: golang array loop with index and value

The range keyword walks the array from index 0 through len(arr)-1. Each iteration gives you the index and a copy of the element (for small types like int or string that copy is cheap).

go
package main

import "fmt"

func main() {
	strArr := [5]string{"Ana", "Bob", "Clair", "Daniel", "East"}
	fmt.Println("String array:")
	for index, element := range strArr {
		fmt.Println("At index", index, "element is", element)
	}

	strInt := [5]int{5, 15, 36, 42, 69}
	fmt.Println("Int array:")
	for index, element := range strInt {
		fmt.Println("At index", index, "element is", element)
	}
}
Output

Running the program prints each String array: line with index and name, then each Int array: line with index and number.

Loop over values only (omit the index)

If you only need each element, use the blank identifier _ for the index:

go
package main

import "fmt"

func main() {
	strArr := [5]string{"Ana", "Bob", "Clair", "Daniel", "East"}
	fmt.Println("String array:")
	for _, element := range strArr {
		fmt.Println(element)
	}
}
Output

That prints the five names one per line after the header.


Index loop: golang for loop array with len

The same walk uses a counter and len(arr) (arrays are always fixed length, so len is a constant at compile time for array types):

go
package main

import "fmt"

func main() {
	strArr := [5]string{"Ana", "Bob", "Clair", "Daniel", "East"}
	fmt.Println("String array:")
	for i := 0; i < len(strArr); i++ {
		fmt.Println("At index", i, "element is", strArr[i])
	}

	strInt := [5]int{5, 15, 36, 42, 69}
	fmt.Println("Int array:")
	for i := 0; i < len(strInt); i++ {
		fmt.Println("At index", i, "element is", strInt[i])
	}
}
Output

The observable output matches the range version above. Use this style when you need the index arithmetic explicitly or when stepping multiple arrays together with one i.


Arrays vs slices (one line)

[5]int and []int both support range and len, but an array’s length is part of its type; a slice is a descriptor over some backing array. For append and growth patterns, read a slice-focused article; for golang loop over array on a fixed [n]T, the loops here are all you need.


Summary

Golang iterate over array and go loop over array boil down to for i, v := range arr or for i := 0; i < len(arr); i++ with arr[i]. Golang array loop code almost always uses range for clarity; golang for loop array with len stays useful when the index drives the logic. Dropping the index with _ gives a clean golang loop through array over values only.


References


Frequently Asked Questions

1. How do I golang iterate over array or golang loop through array?

Use for i, v := range arr to get index and value, or for i := 0; i < len(arr); i++ and arr[i] for index-only loops. Both work for fixed-size arrays like [5]int.

2. What is the difference between golang array loop with range and len?

range is idiomatic and harder to get off-by-one wrong; len with an index is familiar from C-style loops and handy when you only need the index or want to walk two arrays in lockstep with the same i.

3. Can I golang loop over array without the index?

Yes: for _, v := range arr { ... } discards the index with the blank identifier.

4. Is golang iterate array the same for slices?

The same for and range forms work on slices; arrays have a fixed length in the type [n]T while slices are []T. This page focuses on arrays; see slice guides for append and capacity.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …