github.com/mailgun/holster/v4@v4.20.0/slice/remove.go (about) 1 package slice 2 3 // Remove removes the given index range from the given slice, preserving element order. 4 // It is the safer version of slices.Delete() as it will 5 // not panic on invalid slice ranges. Nil and empty slices are also safe. 6 // Remove modifies the contents of the given slice and performs no allocations 7 // or copies. 8 func Remove[T comparable](slice []T, i, j int) []T { 9 // Nothing to do for emtpy slices or starting indecies outside range 10 if len(slice) == 0 || i > len(slice) { 11 return slice 12 } 13 // Prevent invalid slice indexing 14 if i < 0 || j > len(slice) { 15 return slice 16 } 17 // Removing the last element is a simple re-slice 18 if i == len(slice)-1 && j >= len(slice) { 19 return slice[:i] 20 } 21 // Note: this modifies the slice 22 return append(slice[:i], slice[j:]...) 23 }