Created
May 2, 2017 08:45
-
-
Save wejick/b517975483ce84e677fb96ef8fd0c51a to your computer and use it in GitHub Desktop.
[golang] Removing element from slice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not sure how to do the second one without allocating new slice