[转] Go Slice 秘籍
Golang官方总结: Slice Tricks
由于引入了内建的append的方法, 包container/vector的很多方法都被移除,可以被内建的append和copy方法代替。
下面是栈vector的操作方法的实现,使用slice实现相关的操作。
1. Append Vector
Section titled “1. Append Vector”a = append(a, b...)2. Copy
Section titled “2. Copy”b = make([]T, len(a))copy(b, a)//如果a不为空, 等效实现b = append([]T(nil), a...)3. Cut
Section titled “3. Cut”a = append(a[:i], a[j:]...)4. Delete
Section titled “4. Delete”a = append(a[:i], a[i + 1]...)// 或者a = a[:i + copy(a[i:], a[i + 1])]5. Delete,而不保持原有顺序
Section titled “5. Delete,而不保持原有顺序”a[i] = a[len(a) - 1]a = a[:len(a) - 1]注意:如果需要被GC回收的元素是一个指针,或者struct含有指针字段,上面的cut,delete实现可能就导致内存泄漏:一些元素的值会被a一直引用而不会被回收。下面的实现可以解决这个问题:
copy(a[i:], a[j:])for k,n := len(a)-j+i, len(a); k<n; k++ { a[k] = nil}a = a[:len(a)-j+i]Delete
Section titled “Delete”copy(a[i:], a[i+1:])a[len(a)-1] = nila = a[:len(a)-1]Delete 而不保持原来的顺序
Section titled “Delete 而不保持原来的顺序”a[i] =a[len(a)-1]a[len(a)-1]=nila = a[:len(a)-1]6.Expand
Section titled “6.Expand”a = append(a[:i], append(make([]T, j), a[i:]...)...)7.Extend
Section titled “7.Extend”a = append(a, make([]T, j)...)8.Insert
Section titled “8.Insert”a = append(a[:i], append([]T{x}, a[i:]...)...)注意 : 第二个append使用自己的底层存储创建一个新的slice,然后复制a[:i]中的元素到这个slice中,然后再把这些元素复制回a。新slice的创建和第二次的复制通过另外一种方式避免:
s = append(s, 0)copy(s[i+1:], s[i:])s[i] = x9. Insert Vector
Section titled “9. Insert Vector”a = append(a[:i], append(b, a[i:]...)...)10. Pop
Section titled “10. Pop”x, a = a[len(a)-1], a[:len(a)-1]11. Push
Section titled “11. Push”a = append(a, x)12. Shift
Section titled “12. Shift”x, a := a[0], a[1:]13. Unshift
Section titled “13. Unshift”a = append([]T{x}, a...)14.其他技巧
Section titled “14.其他技巧”无新内存分配的过滤
Section titled “无新内存分配的过滤”这个技巧利用是: slice共享底层的array和存储容量。所以,在过滤slice时,会重用底层的存储。与此同时,底层存储的数据必然会被修改。
b := a[:0]for _, x := range a { if f(x) { b = append(b, x) }}使用相同的元素(不分配新的对象)替换slice中内容,并将其反序。
for i:= len(a)/2 - 1; i >= 0; i--{ opp := len(a)-1-i a[i], a[opp] = a[opp], a[i]}下面的代码实现同样的效果,只不过使用了两个索引变量
for left, right := 0, len(a)-1; left < right; left, right = left + 1, right -1 { a[left] , r[right] = a[right], a[left]}