This question already has an answer here:
- Delete element in a slice 6 answers
I have a slice of strings, and I want to remove a specific one.
strings := []string
strings = append(strings, "one")
strings = append(strings, "two")
strings = append(strings, "three")
Now how can I remove the string "two"
from strings
?
Find the element you want to remove and remove it like you would any element from any other slice.
Finding it is a linear search. Removing is one of the following slice tricks:
Here is the complete solution (try it on the Go Playground):
If you want to wrap it into a function:
Using it:
Here is a function to remove the element at a particular index:
Try it on Go Playground