Delete element in a slice
Rather than thinking of the indices in the [a:]
-, [:b]
- and [a:b]
-notations as element indices, think of them as the indices of the gaps around and between the elements, starting with gap indexed 0
before the element indexed as 0
.
Looking at just the blue numbers, it's much easier to see what is going on: [0:3]
encloses everything, [3:3]
is empty and [1:2]
would yield {"B"}
. Then [a:]
is just the short version of [a:len(arrayOrSlice)]
, [:b]
the short version of [0:b]
and [:]
the short version of [0:len(arrayOrSlice)]
. The latter is commonly used to turn an array into a slice when needed.
There are two options:
A: You care about retaining array order:
a = append(a[:i], a[i+1:]...)
// or
a = a[:i+copy(a[i:], a[i+1:])]
B: You don't care about retaining order (this is probably faster):
a[i] = a[len(a)-1] // Replace it with the last one. CAREFUL only works if you have enough elements.
a = a[:len(a)-1] // Chop off the last one.
See the link to see implications re memory leaks if your array is of pointers.
https://github.com/golang/go/wiki/SliceTricks
Where a
is the slice, and i
is the index of the element you want to delete:
a = append(a[:i], a[i+1:]...)
...
is syntax for variadic arguments in Go.
Basically, when defining a function it puts all the arguments that you pass into one slice of that type. By doing that, you can pass as many arguments as you want (for example, fmt.Println
can take as many arguments as you want).
Now, when calling a function, ...
does the opposite: it unpacks a slice and passes them as separate arguments to a variadic function.
So what this line does:
a = append(a[:0], a[1:]...)
is essentially:
a = append(a[:0], a[1], a[2])
Now, you may be wondering, why not just do
a = append(a[1:]...)
Well, the function definition of append
is
func append(slice []Type, elems ...Type) []Type
So the first argument has to be a slice of the correct type, the second argument is the variadic, so we pass in an empty slice, and then unpack the rest of the slice to fill in the arguments.