- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Swift Program to get array elements after N elements
- Golang Program To Find Common Array Elements
- Golang Program to Rotate Elements of an Array
- Golang program to shuffle the elements of an array
- Golang program to remove all elements from an array
- Golang Program To Remove Duplicate Elements From An Array
- Golang Program to Remove Repeated Elements From an Array
- 8086 program to determine modulus of first array elements corresponding to another array elements\n
- C++ program to find array after inserting new elements where any two elements difference is in array
- Golang Program to cyclically permutes the elements of the array
- Golang Program To Copy All The Elements Of One Array To Another Array
- Golang Program To Rotate Matrix Elements
- Program to find mean of array after removing some elements in Python
- Golang Program to find the odd-occurring elements in a given array
- Write a Golang program to find duplicate elements in a given array
