Sample code:
func main() {
bar := []string{"Monday", "Tuesday", "Wednesday"}
slices.Delete(bar, 0, 1) // I want to delete 'Monday'
fmt.Println(bar) // prints [Tuesday Wednesday Wednesday]
}
I don't understand why I get a copy of 'Wednesday'. I was expecting a two-element slice.
CodePudding user response:
slices.Delete
returns the modified slice. You should use it as:
bar=slices.Delete(bar, 0, 1)
This is because the delete operation shifts the elements in the slice, and then returns a shorter slice, but the original slice bar
remains the same.