slice 的结构
    切片的结构体由3部分构成,Pointer 是指向一个数组的指针,len 代表当前切片的长度,cap 是当前切片的容量

    1. // slice的底层实现
    2. type slice struct {
    3. array unsafe.Pointer
    4. len int
    5. cap int
    6. }
    type slice struct {
        array unsafe.Pointer
        len   int
        cap   int
    }
    
    func makeslice(et *_type, len, cap int) unsafe.Pointer {
        mem, overflow := math.MulUintptr(et.size, uintptr(cap))
        if overflow || mem > maxAlloc || len < 0 || len > cap {
            // NOTE: Produce a 'len out of range' error instead of a
            // 'cap out of range' error when someone does make([]T, bignumber).
            // 'cap out of range' is true too, but since the cap is only being
            // supplied implicitly, saying len is clearer.
            // See golang.org/issue/4085.
            mem, overflow := math.MulUintptr(et.size, uintptr(len))
            if overflow || mem > maxAlloc || len < 0 {
                panicmakeslicelen()
            }
            panicmakeslicecap()
        }
    
        return mallocgc(mem, et, true)
    }
    
    func panicmakeslicelen() {
        panic(errorString("makeslice: len out of range"))
    }
    
    func panicmakeslicecap() {
        panic(errorString("makeslice: cap out of range"))
    }
    
    
    func makeslice64(et *_type, len64, cap64 int64) unsafe.Pointer {
        len := int(len64)
        if int64(len) != len64 {
            panicmakeslicelen()
        }
    
        cap := int(cap64)
        if int64(cap) != cap64 {
            panicmakeslicecap()
        }
    
        return makeslice(et, len, cap)
    }
    

    image.png
    当 b := a 时,b不扩容,修改值,相应的slice值也会改变,地址指向的数组改变了

    当 b扩容超过cap,添加值时, 对应的slice值不改变,b对应的数组地址变化了,相当于 b和a 是对应两个 数组地址。

    扩容

    func growslice(et *_type, old slice, cap int) slice {
        if raceenabled {
            callerpc := getcallerpc()
            racereadrangepc(old.array, uintptr(old.len*int(et.size)), callerpc, funcPC(growslice))
        }
        if msanenabled {
            msanread(old.array, uintptr(old.len*int(et.size)))
        }
    
        if cap < old.cap {
            panic(errorString("growslice: cap out of range"))
        }
    
        if et.size == 0 {
            // append should not create a slice with nil pointer but non-zero len.
            // We assume that append doesn't need to preserve old.array in this case.
            return slice{unsafe.Pointer(&zerobase), old.len, cap}
        }
    
        newcap := old.cap
        doublecap := newcap + newcap
        if cap > doublecap {
            newcap = cap
        } else {
            if old.len < 1024 {
                newcap = doublecap
            } else {
                // Check 0 < newcap to detect overflow
                // and prevent an infinite loop.
                for 0 < newcap && newcap < cap {
                    newcap += newcap / 4
                }
                // Set newcap to the requested cap when
                // the newcap calculation overflowed.
                if newcap <= 0 {
                    newcap = cap
                }
            }
        }
    
        var overflow bool
        var lenmem, newlenmem, capmem uintptr
        // Specialize for common values of et.size.
        // For 1 we don't need any division/multiplication.
        // For sys.PtrSize, compiler will optimize division/multiplication into a shift by a constant.
        // For powers of 2, use a variable shift.
        switch {
        case et.size == 1:
            lenmem = uintptr(old.len)
            newlenmem = uintptr(cap)
            capmem = roundupsize(uintptr(newcap))
            overflow = uintptr(newcap) > maxAlloc
            newcap = int(capmem)
        case et.size == sys.PtrSize:
            lenmem = uintptr(old.len) * sys.PtrSize
            newlenmem = uintptr(cap) * sys.PtrSize
            capmem = roundupsize(uintptr(newcap) * sys.PtrSize)
            overflow = uintptr(newcap) > maxAlloc/sys.PtrSize
            newcap = int(capmem / sys.PtrSize)
        case isPowerOfTwo(et.size):
            var shift uintptr
            if sys.PtrSize == 8 {
                // Mask shift for better code generation.
                shift = uintptr(sys.Ctz64(uint64(et.size))) & 63
            } else {
                shift = uintptr(sys.Ctz32(uint32(et.size))) & 31
            }
            lenmem = uintptr(old.len) << shift
            newlenmem = uintptr(cap) << shift
            capmem = roundupsize(uintptr(newcap) << shift)
            overflow = uintptr(newcap) > (maxAlloc >> shift)
            newcap = int(capmem >> shift)
        default:
            lenmem = uintptr(old.len) * et.size
            newlenmem = uintptr(cap) * et.size
            capmem, overflow = math.MulUintptr(et.size, uintptr(newcap))
            capmem = roundupsize(capmem)
            newcap = int(capmem / et.size)
        }
    
        // The check of overflow in addition to capmem > maxAlloc is needed
        // to prevent an overflow which can be used to trigger a segfault
        // on 32bit architectures with this example program:
        //
        // type T [1<<27 + 1]int64
        //
        // var d T
        // var s []T
        //
        // func main() {
        //   s = append(s, d, d, d, d)
        //   print(len(s), "\n")
        // }
        if overflow || capmem > maxAlloc {
            panic(errorString("growslice: cap out of range"))
        }
    
        var p unsafe.Pointer
        if et.ptrdata == 0 {
            p = mallocgc(capmem, nil, false)
            // The append() that calls growslice is going to overwrite from old.len to cap (which will be the new length).
            // Only clear the part that will not be overwritten.
            memclrNoHeapPointers(add(p, newlenmem), capmem-newlenmem)
        } else {
            // Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan uninitialized memory.
            p = mallocgc(capmem, et, true)
            if lenmem > 0 && writeBarrier.enabled {
                // Only shade the pointers in old.array since we know the destination slice p
                // only contains nil pointers because it has been cleared during alloc.
                bulkBarrierPreWriteSrcOnly(uintptr(p), uintptr(old.array), lenmem-et.size+et.ptrdata)
            }
        }
        memmove(p, old.array, lenmem)
    
        return slice{p, old.len, newcap}
    }
    

    扩容策略,
    cap小于1024

    1. 如果 append 后的 len 大于 cap 的2倍,即扩大至大于 len 的第一个2的倍数
    2. 如果 append 后的 len 大于 cap 且小于 cap 的两倍,cap扩大至2倍
    3. 如果 append 后的 len 小于 cap,直接追加

    cap 大于1024

    1. 如果 append 后的 len 大于 cap 且小于 cap 的两倍,cap扩大至1.25倍
    2. 如果 append 后的 len 小于 cap,直接追加