The type [n]T
is an array of n values of type T. The expression var a [10]int
declares a variable as an array of ten integers. For performing operations on arrays, the need arises to iterate over them. In this tutorial, we will walk through how to do that in Golang. Using for loop is best suited for this purpose in golang to iterate over an array.
Different methods to iterate over an array in golang
Method-1: Using for loop with range keyword
Here's an example of using <a href="https://go.dev/ref/spec#For_range">range</a>
to loop through a string and an int
array:
package main
import "fmt"
func main() {
// an string array
strArr := [5]string{"Ana", "Bob", "Clair", "Daniel", "East"}
fmt.Println("String array:")
// using for loop
for index, element := range strArr {
fmt.Println("At index", index, "element is", element)
}
// an int array
strInt := [5]int{5, 15, 36, 42, 69}
fmt.Println("Int array:")
// using for loop
for index, element := range strInt {
fmt.Println("At index", index, "element is", element)
}
}
Output:
String array:
At index 0 element is Ana
At index 1 element is Bob
At index 2 element is Clair
At index 3 element is Daniel
At index 4 element is East
Int array:
At index 0 element is 5
At index 1 element is 15
At index 2 element is 36
At index 3 element is 42
At index 4 element is 69
Explanation:
The keyword range
specifies the range of iterations up to the length of arr. The variables index
and element
store the array's indexes and values, respectively.
We can omit the array's index with the blank identifier _
if we do not need to use it:
package main
import "fmt"
func main() {
// an string array
strArr := [5]string{"Ana", "Bob", "Clair", "Daniel", "East"}
fmt.Println("String array:")
// using for loop
for _, element := range strArr {
fmt.Println(element)
}
}
Output:
String array:
Ana
Bob
Clair
Daniel
East
Method-2: Using for loop with len(array) function
len(arr)
: identify the length of an array. The example below demonstrate a loop to iterate over an array with len(array)
function:
package main
import "fmt"
func main() {
// an string array
strArr := [5]string{"Ana", "Bob", "Clair", "Daniel", "East"}
fmt.Println("String array:")
// using for loop
for i := 0; i < len(strArr); i++ {
fmt.Println("At index", i, "element is", strArr[i])
}
// an int array
strInt := [5]int{5, 15, 36, 42, 69}
fmt.Println("Int array:")
// using for loop
for i := 0; i < len(strInt); i++ {
fmt.Println("At index", i, "element is", strInt[i])
}
}
Output:
String array:
At index 0 element is Ana
At index 1 element is Bob
At index 2 element is Clair
At index 3 element is Daniel
At index 4 element is East
Int array:
At index 0 element is 5
At index 1 element is 15
At index 2 element is 36
At index 3 element is 42
At index 4 element is 69
Explanation:
The variable i
is initialized as 0 increase at every iteration until it reaches the value of the length of the array. Print command is given to print the elements at each index of the array one by one.
With the same idea but different implementation is shown below:
package main
import "fmt"
func main() {
// an string array
strArr := [5]string{"Ana", "Bob", "Clair", "Daniel", "East"}
fmt.Println("String array:")
// using for loop
i := 0
for range strArr {
fmt.Println("At index", i, "element is", strArr[i])
i++
}
}
Summary
We have looked at how to quickly iterate over an array of type strings in Go in this article. To iterate over the int
or string
array that have created, we can use the range
keyword or a loop with the len(array)
function.
References
https://www.golinuxcloud.com/golang-for-loop/
https://go.dev/ref/spec#For_range
How do I iterate over an array of a custom type in Go?
In Go (golang), how to iterate two arrays, slices, or maps using one `range`