Go deep copy and shallow copy

Deep copy and shallow copy

  1. concept

    Deep Copy: copy the data itself

    Value types, the default is a deep copy. array, int, float, bool, string, struct

    Shallow copies: copy address data

    Leading to multiple variables point to the same piece of memory

    Reference data type, the default is shallow copy. slice, map

  2. For deep copy function sliced

    func copy(dst, src []Type) int
    //在目标切片的下标为0的位置,开始粘贴复制的源数据
    //内建函数copy将元素从来源切片复制到目标切片中
    //copy返回被复制的元素数量,它会是 len(src) 和 len(dst) 中较小的那个
    
    copy(dstSlice[n:],srcSlice[m:])
    //通过这种方式,可以将源切片中指定位置的数据拷贝到目的切片的指定位置

Guess you like

Origin www.cnblogs.com/henryno12/p/12381107.html