Golang Program To Get The Last Item From The Array


In this tutorial, we will write a go language program to get the last item from an array. An array is a data structure that is used to store elements in contiguous memory locations. In this article, 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 Last Item from the Array of Integers in the Main

In this method, we will write a golang program to get the first element from the array of integers in the main() section of the program. To achieve the result here the concept of indexing of 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 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 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 the 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 last 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 last array element on the screen.

Example

Golang program to get the last item from the array of integers in the main.

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 = len(array) - 1
   elem := array[index]
   fmt.Println()
   fmt.Println("The element present in the last 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: 97

Method 2: GoLang Program to get the Last Element from the Array Using For Loop

In this method, we will write a go language program to get the last 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 the make() function and append values to the array. Further, print the array on the screen

Step 4 − Store the index of the last 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 the 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 last 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 last 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 last location of array is:", result)
}

Output

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

Conclusion

We have successfully compiled and executed a go language program to get the last 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

928 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements