Golang Program To Push An Array Into Another Array


In this tutorial, we will write a go language program to push an array to another array. There are many methods for this. The simplest one is to use equality operator. Here, we will see 3 methods via which we can transfer the contents of one array to another in go programming language.

Method 1: Push an Array of Strings to an Empty Array by using Equality Operator

In this method, we will use equality operator to add the contents of one array to another in the main() section of the program. Note that while transferring elements via this method the elements of the new array will be overwritten.

Syntax

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.

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 slice that we can store in the variable.

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − Now, start the main() function. Inside this function create two arrays of string data type using make() function and store data in one of these arrays

Step 3 − Then, print these arrays on the screen using fmt.Println() function. After that use equality (==) operator to copy the contents of first array to second one.

Step 4 − Once the array is copied then print the new array thus formed on the screen.

Example

Golang program to push an array of strings to an empty array by using equality operator.

package main
import "fmt"
func main() {
   
   // initializing an array
   array := make([]string, 0, 3)
   array1 := make([]string, 0, 3)
   array = append(array, "Apple", "Mango", "Banana")
   fmt.Println("The first array is:", array)
   fmt.Println("The second array is:", array1)
   
   // copying array contents
   array1 = array
   fmt.Println()
   
   // printing the array
   fmt.Println("The array obtained after copying the contents is:", array1)
}

Output

The first array is: [Apple Mango Banana]
The second array is: []
The array obtained after copying the contents is: [Apple Mango Banana]

Method 2: Push an Array of Strings to Another Array using Copy() Function

In this method, we will use copy() library function in go to push the contents of one array to another. Copy is an inbuilt function in go present in strings package.

Syntax

func copy(dst, str[] type) int

The copy function in go language is used to copy the values of one source array to the destination array and returns the number of elements copied as the result. It takes the two arrays as an argument and this function is present in strings package.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Start the main() function. Inside this function initialize two arrays of string data type and store values to them using append() function.

Step 3 − Now, print both arrays on the screen and use copy function to copy the contents of first array to the second one.

Step 4 − Once the elements are copied store the number of elements copied in a variable called result and print the final array on the screen along with number of elements copied

Example

Golang program to push an array of strings to another array using copy() function

package main
import "fmt"
func main() {
   
   // initializing an array
   array := make([]string, 0, 3)
   var array1 []string
   array = append(array, "Apple", "Mango", "Banana")
   array1 = append(array1, "pine-apple", "Cherry")
   fmt.Println("The first array is:", array)
   fmt.Println("The second array is:", array1)
   
   // copying array contents
   result := copy(array1, array)
   fmt.Println()
   
   // printing the array
   fmt.Println("The array obtained after copying the contents is:", array1)
   fmt.Println("The number of elements copied are:", result)
}

Output

The first array is: [Apple Mango Banana]
The second array is: [pine-apple Cherry]
The array obtained after copying the contents is: [Apple Mango]
The number of elements copied are: 2

Method 3: Push One Array to another using Append() Function

In this method, we will write a go language program to push the elements of one array to another by using the append() function. When we copy the contents of one array to another by using this function, the current elements of the new array are not overwritten.

Algorithm

Step 1 − Import the fmt package.

Step 2 − Start() the main() function and initialize two arrays. Store data in both arrays and print them on the screen using fmt.Println() function.

Step 3 − Then, use the append function to copy the contents of first array to the second one. The first argument to the function is the array to which we wish to copy the values.

Step 4 − once the array contents are copied to the new array, print the new array thus formed on the screen.

Example

Golang program to push one array to another using the append() function.

package main
import "fmt"
func main() {
   
   // initializing an array
   array := make([]string, 0, 3)
   var array1 []string
   array = []string{"Apple", "Mango", "Banana"}
   array1 = append(array1, "pine-apple", "Cherry")
   fmt.Println("The first array is:", array)
   fmt.Println("The second array is:", array1)

   // copying array contents
   array1 = append(array1, array...)
   fmt.Println()
   
   // printing the array
   fmt.Println("The array obtained after copying the contents is:", array1)
}

Output

The first array is: [Apple Mango Banana]
The second array is: [pine-apple Cherry]
The array obtained after copying the contents is: [pine-apple Cherry Apple Mango Banana]

Conclusion

We have successfully compiled and executed a go language program to copy the contents of one array to another along with examples. We have made three programs here in the first program we are simply using the equality operator to store the values while in second and third examples we have used go library functions to achieve the result.

Updated on: 10-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements