Golang-Interview icon indicating copy to clipboard operation
Golang-Interview copied to clipboard

194题答案和个人思路(望大家指点)

Open Fishicat opened this issue 4 years ago • 0 comments

输出结果为[]

copy函数实际上会返回一个int值,这个int是一个size,计算逻辑为 size = min(len(dst), len(src)) 这个size的大小,决定了src要copy几个元素给dst 由于题目中,dst声明了,但是没有进行初始化,所以dst的len是0 因此实际没有从src上copy到任何元素给dst

修改版本如下:

func main() {
        var src, dst []int
	src = []int{1, 2, 3}
        //这是未修改前的
	copy(dst, src)
	fmt.Println(dst)

        //这是修改时添加的
	dst = make([]int, len(src))
	copy(dst, src)
	fmt.Println(dst)
}

Fishicat avatar Apr 23 '20 07:04 Fishicat