- 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
How to copy one slice into another slice in Golang?
In Golang, slices are a powerful and flexible data structure that allows you to store a sequence of elements of the same type. When working with slices, you may need to copy one slice into another slice. Fortunately, Golang provides an easy and efficient way to do this.
In this article, we will discuss how to copy one slice into another slice in Golang.
Using The Copy Function in Golang
Golang provides a built-in copy function that allows you to copy the elements of one slice into another slice. The copy function takes two arguments: the destination slice and the source slice. The destination slice should be of the same length or longer than the source slice.
Syntax
Here is the syntax of the copy function −
copy(destination []T, source []T) int
where T is the type of the elements in the slices.
The copy function returns the number of elements that were copied from the source slice to the destination slice.
Example
Here is an example of how to use the copy function to copy one slice into another slice −
package main import "fmt" func main() { slice1 := []int{1, 2, 3, 4, 5} slice2 := make([]int, len(slice1)) copy(slice2, slice1) fmt.Println(slice2) // Output: [1 2 3 4 5] }
Output
[1 2 3 4 5]
In this example, we create two slices slice1 and slice2. slice1 contains the elements 1, 2, 3, 4, 5. We use the make function to create slice2 with the same length as slice1. Then, we use the copy function to copy the elements of slice1 into slice2. Finally, we print the elements of slice2.
Using The Append Function in Golang
Another way to copy one slice into another slice is by using the append function. The append function takes two or more slices as arguments and returns a new slice that contains all the elements of the original slices.
Example
Here is an example of how to use the append function to copy one slice into another slice −
package main import "fmt" func main() { slice1 := []int{1, 2, 3, 4, 5} slice2 := []int{} slice2 = append(slice2, slice1...) fmt.Println(slice2) // Output: [1 2 3 4 5] }
Output
[1 2 3 4 5]
In this example, we create two slices slice1 and slice2. slice1 contains the elements 1, 2, 3, 4, 5. We create an empty slice slice2 and use the append function to append the elements of slice1 to slice2. Finally, we print the elements of slice2.
Conclusion
In conclusion, copying one slice into another slice is a common operation when working with slices in Golang. You can use the built-in copy function or the append function to accomplish this task efficiently. It is important to ensure that the destination slice is of the same length or longer than the source slice.