Golang Program To Get The First Item From The Array


In this tutorial, we will write a go language program to get the first item from an array. An array is a data structure that is used to store elements in contiguous memory locations. Here, we will write two programs to store the first element in the array. In the first program, we will use the concept of indexing and in the second we will use the for loops to get the required result.

Method 1: Get the First Item from the Array using Append() Function

In this method, we will write a golang program to get the first element from the array of integers in the main() function of the program. To achieve the result here the concept of indexing arrays is used.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size, and capacity as arguments

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes a number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of an array containing all the values.

Algorithm

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

Step 2 − Now, we need to start the main() function.

Step 3 − Here, initialize an array of integers using make() function and append values to the array. Further, print the array on the screen by using fmt.Println() function.

Step 4 − Store the index of the first element in a variable that should be printed. Store the element present at that index in a variable using name_of_array[index].

Step 5 − Further, print the value present in that variable containing the first array element on the screen.

Example

Golang program to get the first item from the array of integers in the main() function.

package main
import "fmt"
func main() {
  
   // initializing array
   array := make([]int, 0, 8)
   array = append(array, 11, 20, 13, 44, 56, 96, 54, 97)
   fmt.Println("The given array is:", array)
   
   // getting the first element
   var index int = 0
   elem := array[index]
   fmt.Println()
   fmt.Println("The element present in the first location of array is:", elem)
}

Output

The given array is: [11 20 13 44 56 96 54 97]
The element present in the first location of array is: 11

Method 2: Get the First Element from the Array using For Loop

In this method, we will write a go language program to get the first element from the array using for loops in the main() section of the program.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size, and capacity as arguments.

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes a number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of an array containing all the values.

Algorithm

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

Step 2 − Now, we need to start the main() function.

Step 3 − Here, initialize an array of integers using make() function and append values to the array. Further, print the array on the screen

Step 4 − Store the index of first element in a variable that should be printed.

Step 5 − Now, use for loop to iterate over the array and ignore the iteration values greater than index else store the element in a new variable called result.

Step 6 − Further, print the final result on the screen.

Example

Golang program to get the first element from the array using for loop.

package main
import "fmt"
func main() {

   // initializing array
   var result int
   array := make([]int, 0, 8)
   array = append(array, 11, 20, 13, 44, 56, 96, 54, 97)
   fmt.Println("The given array is:", array)
   var index int = 0
   
   // getting the first element
   for i := 0; i < len(array); i++ {
      if i > index {
         continue
      } else {
         result = array[i]
      }
   }
   fmt.Println()
   fmt.Println("The element present in the first location of array is:", result)
}

Output

The given array is: [11 20 13 44 56 96 54 97]
The element present in the first location of array is: 11

Conclusion

We have successfully compiled and executed a go language program to get the first item from the array along with examples. We have used two functions for this. The first program runs in constant time that is the time complexity of that program is O(1) whereas the time complexity of the second program is O(n2).

Updated on: 10-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements