|
SliceTricks
How to do vector-esque things with slices.
slices 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. |