Replacing container/vector in Go programs

25 Oct 2011

In this commit, one of Go’s widely used packages, container/vector was deleted. It may be confusing at first, but in reality you can achieve everything in container/vector with slice tricks. There’s a list of slice tricks here, but I’ll reproduce them on this blog.

//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[len(a)-1]
a = a[:len(a)-1]

//Push
a = append(a, x)

A lot of my programs used vector.Push, and converting everything to use append made everything a lot cleaner.