Skip to content

Instantly share code, notes, and snippets.

@wejick
Created May 2, 2017 08:45
Show Gist options
  • Save wejick/b517975483ce84e677fb96ef8fd0c51a to your computer and use it in GitHub Desktop.
Save wejick/b517975483ce84e677fb96ef8fd0c51a to your computer and use it in GitHub Desktop.
[golang] Removing element from slice
//RemoveFromSlice remove string from slice, return number of removed items
func RemoveFromSlice(slice []string, toRemove string) (removed []string, n int) {
for _, i := range slice {
if i != toRemove {
removed = append(removed, i)
} else {
n++
}
}
return
}
//RemoveNFromSliceString remove member nth from slice
func RemoveNFromSliceString(slice []string, n int64) (newSlice []string) {
newSlice = make([]string, len(slice)-1)
newSlice = append(slice[:n], slice[n+1:]...)
return
}
@wejick
Copy link
Author

wejick commented May 2, 2017

not sure how to do the second one without allocating new slice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment