My favorites | Sign in
Project Home Wiki
Search
for
SliceTricks  
How to do vector-esque things with slices.
slices
Updated Jul 12, 2011 by a...@golang.org

Since the introduction of the append built-in, most of the functionality of the container/vector package can be replicated using append and copy.

Here are the vector methods and their slice-manipulation analogues:

AppendVector

a = append(a, b...)

Copy

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

Cut

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

Delete

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

Expand

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

Extend

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

Insert

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

InsertVector

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

Pop

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

Push

a = append(a, x)

Source: this reddit post.

Powered by Google Project Hosting