golang copy 切片

package main

import "fmt"

type Test struct {
    A string
}
func main()  {
    test1()
    test2()
}

func test1() {
    a := []int{1,2,3,4}
    var b []int
    b = make([]int, len(a))
    copy(b, a)
    fmt.Println("b: ", b)
}

func test2() {
    a := []int{1,2,3,4}
    var b []int
    copy(b, a)
    fmt.Println("b: ", b)
}

result; 

b: [1 2 3 4]
b: []

posted @ 2021-10-19 21:13  Black_Climber  阅读(120)  评论(0编辑  收藏  举报