Golang Program to insert multiple elements into the array from the specified index


In this tutorial, we will write a go language program to insert multiple elements into an array at the specified index. There are many methods to add elements into an array. it can be done by either using indexing or by using for loops. There are some inbuilt functions too using which we can add elements at specified index.

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 two arrays of integers using make() function and append values to the array. Further print these arrays on the screen.

STEP 4 − Now, create an empty array of integers and using append function copy all the elements of first array before second element to it and add the element present in fourth location of second array to it.

STEP 5 − Now, copy all the remaining elements of first array and add them to the new array formed.

STEP 6 − Further, overwrite the elements of first array with the array just created using equality operator.

Example

In this example, we will write a go language program to add element at a specified index of an array from second array using internal functions. we will append the second element of the first array from the fourth element of another 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)
   array2 := make([]int, 0, 4)
   array2 = append(array2, 2, 4, 6, 8)
   fmt.Println("The second array is:", array2)
   fmt.Println()
   var array3 []int
   array3 = append(array3, array1[:1]...)
   array3 = append(array3, array2[3])
   array3 = append(array3, array1[1:]...)
   array1 = array3
   fmt.Println("The new array formed after adding the element at 3rd position of second array to first position of the first array is:", array1)
}

Output

The first array is: [11 20 13 44 56 96 54 97]
The second array is: [2 4 6 8]

The new array formed after adding the element at 3rd position of second array to first position of the first array is: [11 8 20 13 44 56 96 54 97]

Conclusion

We have successfully compiled and executed a go language program to add elements into an array at specified index along with examples. we have used the concept of indexing to add element at a particular index.

Updated on: 09-Feb-2023

755 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements