Junior — Middle
Kas teil on praktilisi kogemusi funktsiooniga copy Go keeles?
sobes.tech AI
Vastus AI-lt
In Go, the copy function is used to copy elements from one slice to another. It returns the number of elements successfully copied, which is useful when working with partially filled slices.
Example of usage:
package main
import "fmt"
func main() {
src := []int{1, 2, 3, 4}
dst := make([]int, 2)
n := copy(dst, src)
fmt.Println(dst) // [1 2]
fmt.Println(n) // 2
}
Main points:
copycopies the minimum of the source and destination lengths.- It can be used to create copies of slices to avoid modifying the original data.
- When copying slices of different lengths, only the available elements are copied.