Golang Program to add an element in the array at the beginning


In this tutorial, we will write a go language program to add an element in the array at the beginning of an array. to add an element in the starting of an array we will use for loops and the concept of indexing in arrays. We shall now discuss these methods one by one in this program.

Method 1: Using Append() 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 and returns the resultant variable.

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, start the main() function. Inside the main() function initialize an array of integers using make() function and store values to them using append() function. Further, print this array on the screen using fmt.Println() function.

STEP 3 − In order to store the given element at the first index. Store the value at that index in a variable.

STEP 4 − Now, use append() function to append the given value in the starting of the array. note that there is a slight change in the syntax of append() function in this case.

STEP 5 − At last now print the element we have added on the screen using fmt.Println() function along with the array that is appended.

Example

In this example we will write a go language program to add an element in the starting of an array using for loops.

package main
import "fmt"
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)

   // storing value to be stored
   var ele int = 39
   array = append([]int{ele}, array...)
   fmt.Println()
   fmt.Println("The element to be added is:", ele)
   fmt.Println("Array obtained after adding", ele, "to first position in the above array is: \n", array)
}

Output

The given array is: [1 2 3 4 5]

The element to be added is: 39
Array obtained after adding 39 to first position in the above array is: 
 [39 1 2 3 4 5]

Method 2: Using the Concept of Indexing

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 and returns the resultant variable.

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 make() function and append values to the array. Further print the array on the screen.

STEP 4 − Now, store an element to be stored in the starting of the array in a variable and print it on the screen.

STEP 5 − Now, create an empty array of integers and using append function store the element stored above as the first element of the array followed by all the elements of the first array.

STEP 6 − Further, print the final array on the screen along with the element that was added.

Example

In this example we will use the concept of indexing to add an element in the beginning of the array.

package main
import (
   "fmt"
)
func main() {

   // initializing array
   array1 := make([]int, 0, 8)
   array1 = append(array1, 11, 20, 13, 44, 56, 96, 54, 97)
   fmt.Println("The first array is:", array1)

   // element to be inserted
   var element int = 39
   fmt.Println()
   var array3 []int
   array3 = append(array3, element)
   array3 = append(array3, array1...)
   fmt.Println("The new array formed after adding", element, "in the first position of the array is:", array3)
}

Output

The first array is: [11 20 13 44 56 96 54 97]

The new array formed after adding 39 in the first position of the array is: [39 11 20 13 44 56 96 54 97]

Conclusion

We have successfully compiled and executed a go language program to add an element in the array at the beginning. We have used two approaches in this in the first approach we are using the inbuilt function of go to get the result while in the second one we are using the concept of indexing to achieve the result.

Updated on: 10-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements