go 切片append方法

作者: cheyongzi | 来源:发表于2017-10-11 16:47 被阅读201次
  1. append新建newSlice查看 newSlice和slice之间的区别
slice := make([]int, 5)
fmt.Printf("slice cap:%d, slice length:%d, slice:%v\n",cap(slice), len(slice), slice)
//slice cap:5, slice length:5, slice:[0 0 0 0 0]
newSlice := append(slice, 2)
fmt.Printf("slice cap:%d, slice length:%d, slice:%v\n",cap(slice), len(slice), slice)
fmt.Printf("newslice cap:%d, newslice length:%d, newSlice:%v\n",cap(newSlice), len(newSlice), newSlice)
控制台输出:
slice cap:5, slice length:5, slice:[0 0 0 0 0]
newslice cap:10, newslice length:6, newSlice:[0 0 0 0 0 2]
//append方法会新建一个切片,切片与原切片公用一个底层数组
  1. 通过区间获取newSlice位置1和2两个元素生成otherSlice,append方法增加一个元素4并生成新的切片otherSlice2
otherSlice := newSlice[1:3]
otherSlice2 := append(otherSlice, 4)
fmt.Printf("slice cap:%d, slice length:%d, slice:%v\n",cap(slice), len(slice), slice)
fmt.Printf("newslice cap:%d, newslice length:%d, newSlice:%v\n",cap(newSlice), len(newSlice), newSlice)
fmt.Printf("otherslice cap:%d, otherslice length:%d, otherslice:%v\n",cap(otherSlice), len(otherSlice), otherSlice)
fmt.Printf("otherslice2 cap:%d, otherslice2 length:%d, otherslice2:%v\n",cap(otherSlice2), len(otherSlice2), otherSlice2)
控制台输出:
slice cap:10, slice length:5, slice:[0 0 0 4 0]
newslice cap:10, newslice length:6, newSlice:[0 0 0 4 0 2]
otherslice cap:9, otherslice length:2, otherslice:[0 0]
otherslice2 cap:9, otherslice2 length:3, otherslice2:[0 0 4]

可以看到slice,newslice第四个位置和otherslice2的第三个位置的值都变成了4,因为otherslice是从newslice第一个位置开始获取的数据,所以otherslice的第三个位置相当于newslice的第四个位置
针对1和2两种append方法造成的不同 个人理解  当增加的数据索引在原始切片长度内,则会对原始切片的对应位置造成影响,如果增加的数据的索引超过了原始切片长度,则不会对原始切片造成影响
  1. 修改otherSlice2的第一个元素为10
otherSlice2[0] = 10
fmt.Printf("slice cap:%d, slice length:%d, slice:%v\n",cap(slice), len(slice), slice)
fmt.Printf("newslice cap:%d, newslice length:%d, newSlice:%v\n",cap(newSlice), len(newSlice), newSlice)
fmt.Printf("otherslice cap:%d, otherslice length:%d, otherslice:%v\n",cap(otherSlice), len(otherSlice), otherSlice)
fmt.Printf("otherslice2 cap:%d, otherslice2 length:%d, otherslice2:%v\n",cap(otherSlice2), len(otherSlice2), otherSlice2)
控制台输出:
slice cap:10, slice length:5, slice:[0 10 0 4 0]
newslice cap:10, newslice length:6, newSlice:[0 10 0 4 0 2]
otherslice cap:9, otherslice length:2, otherslice:[10 0]
otherslice2 cap:9, otherslice2 length:3, otherslice2:[10 0 4]

这个例子更好的体现出slice,newslice,otherslice,otherslice2共用一个底层数组
  1. 函数传值
fmt.Printf("otherSlice2 Point:%p\n",&otherSlice2)
modifySlice(otherSlice2)
fmt.Printf("slice cap:%d, slice length:%d, slice:%v\n",cap(slice), len(slice), slice)
fmt.Printf("newslice cap:%d, newslice length:%d, newSlice:%v\n",cap(newSlice), len(newSlice), newSlice)
fmt.Printf("otherslice cap:%d, otherslice length:%d, otherslice:%v\n",cap(otherSlice), len(otherSlice), otherSlice)
fmt.Printf("otherslice2 cap:%d, otherslice2 length:%d, otherslice2:%v\n",cap(otherSlice2), len(otherSlice2), otherSlice2)
          
func modifySlice(slice []int) {
      fmt.Printf("modify function Point:%p\n",&slice)
      slice[1] = 20
      slice = append(slice,50,60)
}
控制台输出:
otherSlice2 Point:0xc42000a300
modify function Point:0xc42000a420
slice cap:10, slice length:5, slice:[0 10 20 4 50]
newslice cap:10, newslice length:6, newSlice:[0 10 20 4 50 60]
otherslice cap:9, otherslice length:2, otherslice:[10 20]
otherslice2 cap:9, otherslice2 length:3, otherslice2:[10 20 4]

相关文章

  • go 切片append方法

    append新建newSlice查看 newSlice和slice之间的区别 通过区间获取newSlice位置1和...

  • 15.Go_Slice(切片)

    Go 切片 定义切片 切片初始化 len()和cap()函数 空(nil)切片 切片拦截 append() 和co...

  • 第03天(复合类型)_04

    18_切片和底层数组关系.go 19_append函数的使用.go 20_append扩容特点.go 21_cop...

  • go 中的slice 合并

    go 中的切片合并即两个 slice 相加使用 go 语言内置的append 方法,我们比较常用的是 添加单个元素...

  • Go中的切片append

  • Python-2-list

    切片请看 Python str list 方法描述·例子备注append向末尾添加值list.append('')...

  • 数组和切片区别

    数组赋值会直接拷贝一份,而切片赋值会传指针。 函数传参,同上 当切片append的时候,go会考虑切片的cap值,...

  • golang 切片在函数传递

    背景: 切片当参数传递时,无法append 原因: go语言中切片是地址传递,test函数添加的1,2,3后被分配...

  • go slice append

    go slice append 切片的底层是一个数组,如果截取切片的一部分赋给另一个切片,那这两个切片的数据其实指...

  • golang系列教程

    Go包管理 Go开发工具 Go Doc 文档 Go 数组 Go 切片 Go Map Go 类型 Go 函数方法 G...

网友评论

    本文标题:go 切片append方法

    本文链接:https://www.haomeiwen.com/subject/urbwyxtx.html