
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get First N Items from an Array in Go
In this tutorial, we will write a go language program to get the first given number of items from an array. We can do this by either using the inbuilt functions in go or by 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.
Method 1: Getting items from an array of integers using internal functions
In this method, we will write a go language program to get the first given number of items from an array by using the append() library function.
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 before which the elements should be stored. Store the element present at that index in a variable.
STEP 6 ? Now, call the delFirstElem() function by passing the array and the index as arguments to the function and store the array obtained.
STEP 7 ? Print the final array on the screen.
Example
Golang program to get the first given number of items from an array of integers using internal functions
package main import "fmt" // function to get the first element from the array func getFirstElem(array []int, index int) []int { return append(array[:index]) } func main() { // initializing an array array := make([]int, 0, 5) array = append(array, 1, 2, 3, 4, 5) fmt.Println("The given array is:", array) var index int = 2 elem := array[index] result := getFirstElem(array, index) fmt.Println() fmt.Println("The provided index is:", index) fmt.Println("Array obtained before the element", elem, "is:\n", result) }
Output
The given array is: [1 2 3 4 5] The provided index is: 2 Array obtained before the element 3 is: [1 2]
Method 2: Get elements from an array using a user-defined function
In this example we will write a go language program to get the first given elements from an array in the main() section of the program.
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 in a variable before which the elements should be stored. Store the element present at that index in a variable.
STEP 5 ? Now, to re-slice the array use the : operator and store values present before the provided index in a variable.
STEP 6 ? Further, print the final array on the screen.
Example
Get the first given elements from an array using user-defined function.
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 = 3 elem := array[index] result := array[:index] fmt.Println() fmt.Println("The provided index is:", index) fmt.Println("Array obtained after getting values before", elem, "is:\n", result) }
Output
The given array is: [11 20 13 44 56] The provided index is: 3 Array obtained after getting values before 44 is: [11 20 13]
Method 3: Get items from an array of integers using append() function
In this method, we will use for loop to get the values before a particular index from the start of an array in go programming language.
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 and value at that index in a variable before which the elements should be printed.
STEP 5 ? Now, use for loop to iterate over the array and store the elements present in places before the provided index in the new array.
STEP 6 ? Further, print the final array on the screen.
Example
Golang program to get the first given number of items from an array of integers using append() function.
package main import "fmt" func main() { array := make([]int, 0, 8) var result []int array = append(array, 11, 20, 13, 44, 56, 96, 54, 97) fmt.Println("The given array is:", array) // getting the index var index int = 3 // re-slicing the array for i := 0; i < len(array); i++ { if i < index { result = append(result, array[i]) } } elem := array[index] fmt.Println() fmt.Println("The provided index is:", index) fmt.Println("Array obtained after getting elements before", elem, "is:\n", result) }
Output
The given array is: [11 20 13 44 56 96 54 97] The provided index is: 3 Array obtained after getting elements before 44 is: [11 20 13]
Conclusion
We have successfully compiled and executed a go language program to get the first given number of items in the array along with examples.