• golang的切片使用总结一


    举例1:make([]int, b)

    s := make([]int, 10)
    fmt.Printf("s:%v, len of s:%v, cap of s:%v   \n", s, len(s), cap(s))

    打印结果:s:[0 0 0 0 0 0 0 0 0 0], len of s:10, cap of s:10

    结论:make([]int, 10) 创建的切片是capacity(容量)为10,且len(长度)为10,只是10个元素均为0

    举例2:make([]int, b)后再append

    s := make([]int, 10)
    s = append(s, 10)
    fmt.Printf("s:%v, len of s:%v, cap of s:%v   \n", s, len(s), cap(s))

    打印结果:s:[0 0 0 0 0 0 0 0 0 0 10], len of s:11, cap of s:20

    结论:向一个capacity为10,len为10的切片追加元素,由于capacity不足,引发扩容,切片会采用原来2倍的容量作为新的容量,并把原来的元素拷贝到新空间,再追加新元素。所以元素为11个,capacity为20

    举例3:make([]int, 0, b)

    s := make([]int, 0, 10)
    fmt.Printf("s:%v, len of s:%v, cap of s:%v   \n", s, len(s), cap(s))

    打印结果:s:[], len of s:0, cap of s:10 

    结论:make([]int, 0, 10) 创建的切片是capacity(容量)为10,但len(长度)为0,即没有元素

    举例4:make([]int, a, b)

    s := make([]int, 10, 12)
    fmt.Printf("s:%v, len of s:%v, cap of s:%v   \n", s, len(s), cap(s))

    打印结果:s:[0 0 0 0 0 0 0 0 0 0], len of s:10, cap of s:12

    结论:make([]int, 10, 12) 创建的切片是capacity(容量)为12,len(长度)为10,且10个元素都为0

    举例5:make([]int, 0, b)后再append

    s := make([]int, 0, 10)
    s = append(s, 1)
    fmt.Printf("s:%v, len of s:%v, cap of s:%v   \n", s, len(s), cap(s))

    打印结果:s:[1], len of s:1, cap of s:10

    结论:向一个capacity为10,len为0的切片中追加元素时,由于capacity足够,可以直接追加,所以最终len为1,capacity仍然为10

    举例6:make([]int, a, b)后再截取

    s := make([]int,10,12)  
    s1 := s[8:]
    fmt.Printf("s1: %v, len of s1: %d, cap of s1: %d",s1,len(s1),cap(s1))

    打印结果:s1:[0 0], len of s1:2, cap of s1:4

    结论:make([]int, 10, 12)创建的是capacity为12,len为10的切片s,s1从下标为8的位置开始截取,所以实际只截取到s[8],s[9]两个元素,长度为2。s1的capacity是根据截取起始位置算的,所以capacity为4

    举例7:make([]int, a, b)后再截取

    s := make([]int,10,12)  
    s1 := s[8:9]
    fmt.Printf("s1: %v, len of s1: %d, cap of s1: %d",s1,len(s1),cap(s1))

    打印结果:s1:[0], len of s1:1, cap of s1:4 

    结论:s是一个capacity为12,len为10的切片,s1从下标为8的位置开始截取,下标为9的的位置结束,由于左边是闭区间,右边是开区间,所以只截取到一个元素,即s1的len为1。但是s1的capacity是根据截取起始位置算的,所以capacity为4

    举例8:make([]int, a, b)后截取,再修改新切片

    s := make([]int, 10, 12)
    s1 := s[8:]
    s1[0] = -1
    fmt.Printf("s:%v", s)

    打印结果:s:[0 0 0 0 0 0 0 0 -1 0]

    结论:截取成新切片后,修改新切片的值,发现旧切片的值也变了。所以新旧切片共享同一块内存

    本篇总结:

    1. 创建切片的语法为:make([]int, a,b),参数a表元素个数,即len值,参数b表切片容量,即capacity值。

    2. 当不传a值时,即格式为make([]int,b)时,表创建一个capacity为b的,len也为b的切片,元素全都为0

    3. 当向一个切片append元素时,若capacity足够,则直接压入切片;若capacity不足时,则重新申请原2倍的空间,先把旧数据copy到新空间,再压入新元素

    4. 截取切片时,无论截取多少元素,新切片的capacity是从原切片的起始位置算的,直到原切片的结尾

    5. 截取切片时,新旧切片共享同一块内存

    golang的切片进阶篇,请参考下一篇博客 golang的切片使用总结二_panic: runtime error: index out of range [10] with-CSDN博客

  • 相关阅读:
    torch-scatter安装失败解决办法
    利用BACnet分布式IO控制器优化Niagara楼宇自动化系统
    Kotlin开发Android之基础问题记录
    Spring Data JPA 中的分页和排序
    01- SA8155P QNX LA/LV 启动(01) - startup
    【C语言】memmove()函数(拷贝重叠内存块函数详解)
    Spring(Ioc)
    Vue中Slot的使用指南
    分享一下拉绳距离编码器的理论常识
    RHCE学习 --- 第五次作业
  • 原文地址:https://blog.csdn.net/yzf279533105/article/details/133581039