深入解析Go语言编程中slice切片结构

2020-01-28 12:02:45于丽


Apppend的用法
 
a := make([]int, 3, 6)
 fmt.Printf("%p", a)
 a = append(a, 1, 2, 3)
 fmt.Printf("%v %pn", a, a)
 a = append(a, 1, 2, 3)
 fmt.Printf("%v %pn", a, a)

我们必须返回切片,因为尽管 Append 可以改变 slice 的元素, 切片自身(持有指针、长度和容量的运行态数据结构)是值传递的。添加切片的主意很有用,因此由内置函数 append 实现。

  func Append(slice, data[]byte) []byte {
      l := len(slice)
      if l + len(data) > cap(slice) {  // reallocate
          // Allocate double what's needed, for future growth.
          newSlice := make([]byte, (l+len(data))*2)
          // Copy data (could use bytes.Copy()).
          for i, c := range slice {
              newSlice[i] = c
          }
          slice = newSlice
      }
      slice = slice[0:l+len(data)]
      for i, c := range data {
          slice[l+i] = c
      }
      return slice
  }

当slice中append追加的元素超过了指向的容量,就会重新指向一个新的底层数组,所以一个底层数组的改变,不会带动其他的改变,试一下下边的代码

a := []int{1, 2, 3, 4, 5}
s1 := a[2:5]
s2 := a[1:3]
fmt.Println(s1, s2)
s2 = append(s2, 1, 2, 2, 3, 3, 4, 5)
s1[0] = 9
fmt.Println(s1, s2)

copy
这是一个拷贝的函数,下边的代码是从s2拷贝到s1然后我们会看到结果是[7 8 9 4 5]
如果是copy(s2,s1) 我们看到的结果是[1 2 3]
 
s1 := []int{1, 2, 3, 4, 5}
s2 := []int{7, 8, 9}
copy(s1, s2)
fmt.Println(s1)