Safely cloning slice can be through many ways, but most common are through copy and slices.Clone. The latter is much simpler because it doesn’t require you to preallocate the destination slice.
func main() {
x := []int{1, 2, 3}
y := make([]int, len(x))
copy(y, x) // Method 1
fmt.Println(x)
fmt.Println(y)
z := slices.Clone(x) // Method 2
fmt.Println(z)
}
Output:
[1 2 3]
[1 2 3]
[1 2 3]