先来看看对copy的错误使用
func main() {
nums:=[]int{1,2,3,4,5}
fmt.Println(" len cap address")
fmt.Print("111---",len(nums),cap(nums))
fmt.Printf(" %p\n",nums)//0xc4200181e0
a:=nums[:3]
fmt.Print("222---",len(a),cap(a))
fmt.Printf(" %p\n",a)//0xc4200181e0 一样
//output: 222--- 3 5
b:=nums[:3:3] //第二个冒号 设置cap的
n:=copy(b,nums[:3:3]) //第二个冒号 设置cap的
fmt.Print("333---",len(b),cap(b))
fmt.Printf(" %p\n",b)//0xc4200181e0 一样
//output: 333--- 3 3
fmt.Println(n,b)
nums[0]=55
fmt.Println(nums,a,b)
}
发现 nums[0]修改了数据后,其他全部都改变了,并且地址都一样,想了想 到底哪里出了问题呢? 是 copy 的问题?
len cap address
111---5 5 0xc4200181e0
222---3 5 0xc4200181e0
333---3 3 0xc4200181e0
3 [1 2 3]
[55 2 3 4 5] [55 2 3] [55 2 3]
发现上面的copy前的对象没有分配内存,使用了一样的内存地址导致的,把上面的代码做一些修改:
b:=nums[:3:3] //第二个冒号 设置cap的
// 修改为
var b =make([]int,len(nums[:3:3]))
这次修改之后,运行的结果就和预期一样了,b的值不会被修改
len cap address
111---5 5 0xc4200181e0
222---3 5 0xc4200181e0
333---3 3 0xc42000a400
3 [1 2 3]
[55 2 3 4 5] [55 2 3] [1 2 3]
golang深拷贝任意结构代码:
// Clone 完整复制数据
func Clone(a, b interface{}) error {
buff := new(bytes.Buffer)
enc := gob.NewEncoder(buff)
dec := gob.NewDecoder(buff)
if err := enc.Encode(a); err != nil {
return err
}
if err := dec.Decode(b); err != nil {
return err
}
return nil
}