Golang Program To Iterate Over Each Element From The Arrays


In this tutorial, we will write a go language program to iterate over each element of the array. An array is a data structure that is used to store data at contiguous memory locations. There are many methods to iterate over an array. The simplest one is to use a for loop. Here, we will see how we can iterate over the integer and string types of arrays using various methods in go.

Method 1: Iterate over the Array using For Loop

In this method, we will write a go language program to iterate over the array using for loops. It should be noted that while iterating over an array it is not necessary to provide a variable to store index or value, we could ignore it by writing a _.

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − Then call the main() function. Inside this function initialize an array of integers and store values to it. Then print the elements of the array on the screen using fmt.Println() function.

Step 3 − Now, using the for loop iterate over each element of the array and print them on the screen using fmt.Println() function.

Example

Golang program to iterate over the array using for loop.

package main
import "fmt"
func main() {
   
   // initializing an array
   arr := []int{1, 2, 3, 4, 5}
   fmt.Println("The given array is:", arr)
   fmt.Println()
   fmt.Println("Printing the array elements after iterating over the array:")
   
   // using for loop
   for _, value := range arr {
      fmt.Println(value)
   }
}

Output

The given array is: [1 2 3 4 5]
Printing the array elements after iterating over the array:
1
2
3
4
5

Explanation

Firstly, we imported the formatted package naming fmt. After that, we started the main() function in which we initialized an array of integers. Then by using the for loop we are storing the value present at each index and then print it on the screen using fmt.Println() function.

Method 2: Iterate Over an Array of Integers using For Loop

In this method, we will use a for loop to iterate over an array of integers using for loops and range() functions. Consider the following code to have more clarity.

Syntax

func len(v Type) int

The len() function is used to get the length of any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value, which is the length of the variable.

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − Then call the main() function. Inside this function initialize an array of integers and store values in it.

Step 3 − Then print the elements of the array on the screen using fmt.Println() function.

Step 4 − Now, using the for loop iterate over each element of the array and print them on the screen using fmt.Println() function.

Example

Golang program to iterate over an array of integers using for loop

package main
import "fmt"
func main() {
   
   // initializing the array
   arr := [5]int{1, 2, 3, 4, 5}
   fmt.Println("The given array is:", arr)
   fmt.Println()
   fmt.Println("Iterate over each element of the array and print its values: ")
   
   // using for loop
   for i := 0; i < len(arr); i++ {
      fmt.Println(arr[i])
   }
}

Output

The given array is: [1 2 3 4 5]
Iterate over each element of the array and print its values:
1
2
3
4
5

Explanation

While iterating over the array using for loop, we first have to choose a variable to iterate and then we provide any value to it followed by the condition when the iteration should stop. In this program, we are getting the last element of the array by using the len() function. In the end, we have to increment or decrement the array variable.

Method 3: GoLang Program to Iterate over an Array of Strings using For Loop

In this method, we will write a go language program to iterate over an array of strings using for loops and range functions.

Syntax

func range(variable)

The range function is used to iterate over any data type. To use this we first have to write the range keyword followed by the data type to which we want to iterate and as a result, the loop will iterate until the last element of the variable.

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − Then Call the main() function. Inside this function initialize an array of strings and store values to it. Then print the elements of the array on the screen using fmt.Println() function.

Step 3 − Now, using the for loop iterate over each element of the array and print each element on the screen using fmt.Println() function.

Step 4 − The index and element variables provided after the for keyword, contain the value of that particular index and value in these variables.

Example

Golang program to iterate over an array of strings using for loop.

package main
import "fmt"
func main() {
   
   // initializing an array
   arr := [5]string{"Ronaldo", "Messi", "Kaka", "James", "Casillas"}
   fmt.Println("The given array is:", arr)
   fmt.Println()
   fmt.Println("Printing the array elements after iterating over the array:")

   // using for loop
   for index, element := range arr {
      fmt.Println("At index", index, "value is", element)
   }
}

Output

The given array is: [Ronaldo Messi Kaka James Casillas]
Printing the array elements after iterating over the array:
At index 0 value is Ronaldo
At index 1 value is Messi
At index 2 value is Kaka
At index 3 value is James
At index 4 value is Casillas

Explanation

First, we imported the formatted package naming fmt. After that, we started the main() function in which we initialized an array of integers. Then by using the for loop we are storing the index and value present at each index and then print it on the screen using fmt.Println() function.

Conclusion

In this tutorial, we have understood three different methods to iterate each element from the arrays. We have used three different examples to demonstrate the iteration of the array of elements and string using for loop.

Updated on: 06-Jan-2023

650 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements