你真的理解 Golang 切片吗?全切片表达式及切片使用技巧

开发 前端
Golang 的切片不仅仅是带长度的指针,它们还有一个 "容量 "字段,因为增加动态分配的数组是一个很常见的任务。分片的容量也作为分片表达式 a[low:high] 的边界检查器——切片的末端不能超过其容量。

简介

Golang 中通常的 slice 语法是 a[low:high],您可能很熟悉。还有另一种切片语法,形式为 a[low:high:max],它采用三个索引而不是两个索引。第三索引 max 是做什么的?

提示: 不是 Python 切片语法 a[low:high:step] 中的 step 步长索引。

答: 第三个索引用于设置切片的容量!在 Golang 规范中称为 “全切片表达式”。

了解 Golang 切片

为了理解为什么要在 Golang 中加入这个功能,以及它的作用,让我们从数组和指针开始。

越界错误在 C 语言程序中很常见,Golang 通过内置的运行时边界检查器来缓解这个问题。数组的边界检查很简单,因为 Golang 的数组是固定长度的,然而,指针的边界检查就不那么简单了,因为指针的边界没有明确定义。Golang 中的切片只是解决指针的边界检查的一种方法。

Golang 不使用普通的指针来访问数组元素,而是用一个长度字段来扩充指针;结果(带长度的指针)被称为 "切片",或在其他地方称为 "胖指针"。有了长度字段,运行时的边界检查就很容易了。

Golang 的切片不仅仅是带长度的指针,它们还有一个 "容量 "字段,因为增加动态分配的数组是一个很常见的任务。分片的容量也作为分片表达式 a[low:high] 的边界检查器——切片的末端不能超过其容量。

理解 a[low:high:max]

切片索引表达式由长度字段进行边界检查,长度字段可以通过分片来减少,以提供所需的边界检查。

同样地,人们可能会想,是否有可能减少片断的容量,以加强对切片表达式 a[low:high] 的边界检查。例如,下面的表达式将一个切片的容量减少到它的长度:

a = a[0:len(a):len(a)]

在这之后,切片 a 被限制在它自己的范围内,切片结束后的元素不能被访问或修改,即使你不小心重新分片或追加到它上面。

这个技巧对于从不可改变的数组中返回一个切片很有用;如果你不小心追加到所谓的不可改变的切片中,就会强制复制,并且没有数据被覆盖,因为已经没有容量了。

这种形式的分片表达式在 Golang 规范中被称为 "完整分片表达式"(full slice expression)。

切片的使用技巧

定义切片:

type SeriesInt64 struct {
   values   []int64
}

自从引入内置的 append 以来,Go 1 中删除的 container/vector 包的大部分功能都可以使用 append 和 copy 来复制。

自从引入泛型以来,golang.org/x/exp/slices 包中提供了其中几个函数的泛型实现。

以下是矢量方法及其切片操作:

AppendVector

a = append(a, b...)

图片

Copy

b := make([]T, len(a))
copy(b, a)

// These two are often a little slower than the above one,
// but they would be more efficient if there are more
// elements to be appended to b after copying.
b = append([]T(nil), a...)
b = append(a[:0:0], a...)

// This one-line implementation is equivalent to the above
// two-line make+copy implementation logically. But it is
// actually a bit slower (as of Go toolchain v1.16).
b = append(make([]T, 0, len(a)), a...)

图片

封装成函数,可以这样写:

func (s *SeriesInt64) copy() *SeriesInt64 {
    if len(s.values) == 0 {
        return &SeriesInt64{
            values:[]int64{},
        }
    }
    // Copy slice
    x := s.values[0 : len(s.values)]
    newSlice := append(x[:0:0], x...)
    return &SeriesInt64{
        values: newSlice,
    }
}

Cut

a = append(a[:i], a[j:]...)

图片

Delete

a = append(a[:i], a[i+1:]...)
// or
a = a[:i+copy(a[i:], a[i+1:])]

图片

封装后:

func (s *SeriesInt64) remove(row int) {
    s.values = append(s.values[:row], s.values[row+1:]...)
}

Delete without preserving order

a[i] = a[len(a)-1] 
a = a[:len(a)-1]

图片

NOTE 如果元素的类型是指针或带指针字段的结构体,需要进行垃圾回收,上述 Cut 和 Delete 的实现存在潜在的内存泄漏问题:一些有值的元素仍然被切片 a 引用,从而无法收集。下面的代码可以解决这个问题:

Cut

copy(a[i:], a[j:])
for k, n := len(a)-j+i, len(a); k < n; k++ {
 a[k] = nil // or the zero value of T
}
a = a[:len(a)-j+i]

图片

Delete

copy(a[i:], a[i+1:])
a[len(a)-1] = nil // or the zero value of T
a = a[:len(a)-1]

Delete without preserving order

a[i] = a[len(a)-1]
a[len(a)-1] = nil
a = a[:len(a)-1]

Expand

Insert n elements at position i:

a = append(a[:i], append(make([]T, n), a[i:]...)...)

Extend

Append n elements:

a = append(a, make([]T, n)...)

Extend Capacity

Make sure there is space to append n elements without re-allocating:

if cap(a)-len(a) < n {
 a = append(make([]T, 0, len(a)+n), a...)
}

Filter (in place)

n := 0
for _, x := range a {
 if keep(x) {
  a[n] = x
  n++
 }
}
a = a[:n]

Insert

a = append(a[:i], append([]T{x}, a[i:]...)...)

注意:第二个追加创建一个新的切片,它有自己的底层存储,并将 a[i:] 中的元素复制到该切片,然后这些元素被复制回切片 a(由第一个追加)。使用替代方法可以避免创建新切片(以及内存垃圾)和第二个副本:

Insert

s = append(s, 0 /* use the zero value of the element type */)
copy(s[i+1:], s[i:])
s[i] = x

封装后:

func (s *SeriesInt64) insert(row int, val int64) {
    s.values = append(s.values, 0)
    copy(s.values[row+1:], s.values[row:])
    s.values[row] = val
}

InsertVector

a = append(a[:i], append(b, a[i:]...)...)

// The above one-line way copies a[i:] twice and
// allocates at least once.
// The following verbose way only copies elements
// in a[i:] once and allocates at most once.
// But, as of Go toolchain 1.16, due to lacking of
// optimizations to avoid elements clearing in the
// "make" call, the verbose way is not always faster.
//
// Future compiler optimizations might implement
// both in the most efficient ways.
//
// Assume element type is int.
func Insert(s []int, k int, vs ...int) []int {
 if n := len(s) + len(vs); n <= cap(s) {
  s2 := s[:n]
  copy(s2[k+len(vs):], s[k:])
  copy(s2[k:], vs)
  return s2
 }
 s2 := make([]int, len(s) + len(vs))
 copy(s2, s[:k])
 copy(s2[k:], vs)
 copy(s2[k+len(vs):], s[k:])
 return s2
}

a = Insert(a, i, b...)

Push

a = append(a, x)

图片

Pop

x, a = a[len(a)-1], a[:len(a)-1]

图片

Push Front/Unshift

a = append([]T{x}, a...)

图片

Pop Front/Shift

x, a = a[0], a[1:]

图片

Prepending

func (s *SeriesInt64) prepend(val int64) {
    if cap(s.values) > len(s.values) {
        s.values = s.values[:len(s.values)+1]
        copy(s.values[1:], s.values)
        s.values[0] = val
        return
    }
    // No extra capacity so a new slice needs to be allocated:
    s.insert(0, val)
}

图片来源:

  • Go Slice Tricks Cheat Sheet
责任编辑:武晓燕 来源: 宇宙之一粟
相关推荐

2021-07-26 22:33:41

切片结构体代码

2013-04-16 10:48:04

Python序列

2022-04-06 08:19:13

Go语言切片

2021-05-24 10:24:42

Golang字符串Python

2022-10-27 19:32:20

切片golang数组

2022-06-02 13:54:04

Go数组切片

2021-05-12 08:47:54

Go数组切片

2011-11-28 15:16:05

Wireshark表达式IP过滤

2023-05-03 09:09:28

Golang数组

2024-03-15 09:56:47

切片函数泛型

2021-08-05 06:54:05

Go切片数据

2021-07-29 10:08:15

NumPy索引切片

2023-04-03 08:02:16

切片扩容GO

2022-09-02 10:20:44

网络切片网络5G

2023-12-27 08:12:04

切片Go语言

2024-01-09 16:14:39

RustGo切片

2023-11-03 14:02:04

Go切片泛型库

2023-09-11 16:16:03

2023-03-29 08:03:53

2019-10-22 22:31:15

Python切片字符串
点赞
收藏

51CTO技术栈公众号