Golang program to fetch elements from an array based on an index


In this tutorial, we will learn how to fetch elements from an array with the help of index using different examples to showcase what methods can be applied to it and the result obtained will be printed on the console using the print function in Golang.

Method 1: Using an Array Index

In this method,we will see how to fetch element from an array using index in for loop and this is one of the simplest methods to solve this problem, lets have a look at this example to understand it.

Algorithm

Step 1 − Create a package main and import fmt package in the program.

Step 2 − Create a function main and further create an array and fill the array with required elements which are to be iterated.

Step 3 − Implement a for loop with index element which is to be returned, print the index element with every array element while traversing.

Step 4 − Execute the print statement using fmt.Println() function in the program.

Example

Golang program to fetch elements using an array index

package main
import "fmt"
func main() {
   fmt.Println("The array values to be printed are:")
   intArray := [5]int{10, 20, 30, 40, 50}
   for index, element := range intArray {
      fmt.Println(index, "=>", element)
   }
}

Output

The array values to be printed are:
0 => 10
1 => 20
2 => 303 => 40
4 => 50

Method 2: Using Slice in an Array

In this method, we will use make function to create a dynamically sized array and the array elements will be printed with respect to slice indexing. Let’s have a look how to do this.

Syntax

func make([]type, length, capacity) []type

Here, make is an inbuilt function in Golang with zeroed array, length and capacity as parameters. The function will use these arguments to create specified slice. A slice will be returned referencing the array, hence it’s a great way to create dynamically sized arrays.

Algorithm

Step 1 − Create a package main and import fmt package in the program.

Step 2 − Using make function create a zeroed array and return slice referencing that array, here according to the example presented we have used the length of slice as 5.

Step 3 − Assign the elements to every index of slice till the length from i.e. from 0 to length-1.

Step 4 − Implement a for loop with index element which is to be returned, print the index element with every slice element while traversing.

Step 5 − Execute the print statement using fmt.Println() function in the program.

Example

Golang program to fetch elements using slice in an array.

package main
import "fmt"
func main() {
   fmt.Println("The elements of array to be printed are: ")
   intSlice := make([]int, 5)
   intSlice[0] = 10
   intSlice[1] = 20
   intSlice[2] = 30
   intSlice[3] = 40
   intSlice[4] = 50
   for index, element := range intSlice {
      fmt.Println(index, "=>", element)
   }
}

Output

The elements of array to be printed are: 
0 => 10
1 => 20
2 => 30
3 => 40
4 => 50

Conclusion

We executed the above program of fetching elements from an array using index with the help of two methods. In the first method we simply used a for loop and returned index with the desired element in the output. In the second method we used make function and returned a slice referencing that array and printed the output with index and its respective element. Consequently, the program executed successfully.

Updated on: 13-Feb-2023

654 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements