Copy List
Copy a list by creating a new list and copying all the values over to the new list.
Examples in
Go
package main
import (
"fmt"
)
func main() {
s := []int{1, 2, 3, 4, 5}
// Method #1
dest := make([]int, len(s))
copy(dest, s)
fmt.Println(dest)
// Method #2
dest2 := append([]int(nil), s...)
fmt.Println(dest2)
// Method #3
dest3 := append(s[:0:0], s...)
fmt.Println(dest3)
}
Last Run
:
[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]