Golang Program to get array elements after N elements


In this tutorial, we will write a go language program to get the last given number of items from an array. We can do this by either using the inbuilt functions in go or using for loops. The first method is more efficient in functionality than the second one but we will discuss both these methods in this 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 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 − Then, create a function to get the elements from a given index. This function accepts the array as an argument and returns the final array.

STEP 3 − Now, we need to start the main() function.

STEP 4 − Here, initialize an array of integers using make() function and append values to the array. further print the array on the screen.

STEP 5 − Store the index in a variable after which the elements printed. Store the elements present at that index in a variable.

STEP 6 − Now, call the getLastElem() function by passing the array and the index as argument to the function and store the array obtained.

STEP 7 − Print the final array on the screen using fmt.Println() function.

Example 1

package main
import "fmt"

// function to get the last element from the array
func getLastElem(array []int, index int) []int {
   return append(array[index:])
}
func main() {

   // initializing an array
   array := make([]int, 0, 5)
   array = append(array, 10, 22, 31, 46, 59)
   fmt.Println("The given array is:", array)
   var index int = 2

   // getting element at index
   elem := array[index]
   result := getLastElem(array, index)
   fmt.Println()
   fmt.Println("The provided index is:", index)
   fmt.Println("Array elements after", elem, "are:\n", result)
}

Output

The given array is: [10 22 31 46 59]

The provided index is: 2
Array elements after 31 are:
 [31 46 59]

Example 2

In this example we will write a go language program to get the last given elements from an array in the main() section of the program.

package main
import "fmt"
func main() {

   // initializing an array
   array := make([]int, 0, 5)
   array = append(array, 11, 20, 13, 44, 56)
   fmt.Println("The given array is:", array)
   var index int = 2
   elem := array[index]
   result := array[index:]
   fmt.Println()
   fmt.Println("The provided index is:", index)
   fmt.Println("Array obtained after getting elements after", elem, "is:\n", result)
}

Output

The given array is: [11 20 13 44 56]

The provided index is: 2
Array obtained after getting elements after 13 is:
 [13 44 56]

Conclusion

We have successfully compiled and executed a go language program to get the number of items from an array after N elements along with examples.

Updated on: 10-Feb-2023

768 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements