2 ms·
Seems you're looking for SliceTricks from the Go wiki: https://code.google.com/p/go-wiki/wiki/SliceTricks https://code.google.com/p/go-wiki/wiki/SliceTricks Ac
by infogulch 12y ago
Seems you're looking for SliceTricks from the Go wiki: https://code.google.com/p/go-wiki/wiki/SliceTricks https://code.google.com/p/go-wiki/wiki/SliceTricks
Actually there are multiple options for delete from a slice. If you need to preserve order then it's going to be an O(n) operation, and if you're deleting often maybe it shouldn't be a slice but some other structure. There are two options for delete mentioned in that document:
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 = a[len(a)-1], nil, a[:len(a)-1]