Function description:
When we need to copy the contents of one slice to another in Go language, we can use the built-in copy() function. The copy() function is used to copy elements from the source slice into the target slice, and it has the following signature:
func copy(dst, src []T) int
Where dst is the target slice, src is the source slice, and T is the type of slice element. The function returns an integer value, indicating the number of elements actually copied (i.e. the minimum length of src and dst).
Note the following points about the behavior of the copy() function:
- The underlying arrays of dst and src must be of the same type. For example, a slice of type []int cannot be copied into a slice of type []string.
- The copy() function does not initialize the slice itself, so before using copy(), you must make sure that the target slice dst has been initialized.
- copy() does not automatically expand: the copy() function will only copy the number of elements that the dst slice can hold. If the capacity of dst is not enough to accommodate all elements of src, the excess elements will be discarded. If you need to copy all elements of the src slice into the dst slice and make sure that dst has sufficient capacity, you need to expand dst before copying. You can use the append() function to achieve the scaling, and then call the copy() function to copy.
- The copy() function will copy elements in src to dst one by one, and will not expand or shrink the slice.
- The copy() function does not create a new slice, it just modifies the contents of the target slice.
Code Example 1:
Here is an example using the copy() function:
package main import "fmt" func main() { // Source slice sourceSlice := []int{1, 2, 3, 4, 5} // Target slice destinationSlice := make([]int, len(sourceSlice)) // Use the copy function to copy the source slice to the target slice numCopied := copy(destinationSlice, sourceSlice) // Print the result ("Source slice:", sourceSlice) ("Destination slice:", destinationSlice) ("Number of elements copied:", numCopied) }
The output may look like this:
Source slice: [1 2 3 4 5]
Destination slice: [1 2 3 4 5]
Number of elements copied: 5
In this example, we first create a slice called sourceSlice, and then use the make function to create a destination slice destinationSlice of the same length as sourceSlice. Next, we use the copy() function to copy the elements in sourceSlice to destinationSlice, and print the content of the two slices and the number of copied elements.
It should be noted that the copy() function does not affect the content of the source slice, it simply copies elements from the source slice into the target slice, so modifying the target slice will not affect the source slice.
Code Example 2:
package main import "fmt" func main() { // Example 1 sourceSlice := []int{1, 2, 3, 4, 5} destinationSlice := make([]int, len(sourceSlice)) numCopied := copy(destinationSlice, sourceSlice) ("Copied elements:", numCopied) // Output: Copied elements: 5 ("Destination slice:", destinationSlice) // Output: Destination slice: [1 2 3 4 5] // Example 2 sourceSlice2 := []string{"apple", "banana", "orange"} destinationSlice2 := make([]string, 2) numCopied2 := copy(destinationSlice2, sourceSlice2) ("Copied elements:", numCopied2) // Output: Copied elements: 2 ("Destination slice:", destinationSlice2) // Output: Destination slice: [apple banana] }
In Example 1, we copy the integer slice sourceSlice into destinationSlice, the length of destinationSlice is the same as sourceSlice, so all elements are copied. In Example 2, the length of the source slice sourceSlice2 is 3, the length of the target slice destinationSlice2 is 2, and only 2 elements are copied.
Code Example 3:
In Go, after copying a slice using the copy function, the old and new slices are independent, they have different memory addresses and are not associated. The copy operation creates a new slice and copies elements from the original slice into the new slice, with the new slice and the original slice pointing to different underlying arrays.
Let's illustrate this situation with an example:
package main import "fmt" func main() { // Original slice originalSlice := []int{1, 2, 3, 4, 5} // Copy slices copiedSlice := make([]int, len(originalSlice)) copy(copiedSlice, originalSlice) // Print the original slice and copy slice address ("Address of originalSlice: %p\n", originalSlice) ("Address of copiedSlice: %p\n", copiedSlice) }
The output may look like this:
Address of originalSlice: 0xc0000b2000
Address of copiedSlice: 0xc0000b2080
In this example, we create a slice called originalSlice, and use the make function to create a new slice copiedSlice, and then use the copy function to copy the element of originalSlice to copiedSlice. Next, we print the addresses of originalSlice and copiedSlice.
As you can see, the addresses of originalSlice and copiedSlice are different, which indicates that they point to different underlying arrays respectively and have no shared memory.
Therefore, after copying the slice through the copy function, the new slice and the original slice are completely independent. Modifying the new slice will not affect the original slice, and there is no correlation between them.
This is the article about the specific use of go built-in function copy(). For more related contents of go built-in function copy(), please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!