How to Delete an Element from a Slice in Go

Delete a Go slice element by swapping it with the last item and truncating the slice length.

You delete an element from a Go slice by replacing it with the last element and then truncating the slice length.

i := 2 // index to delete
mySlice[i] = mySlice[len(mySlice)-1]
mySlice = mySlice[:len(mySlice)-1]

This method preserves order only if you don't care about the original sequence, as it swaps the target with the final item.