fix(slice): return safely when DeleteAt receives no elements (#381)

This commit is contained in:
Yusuf Efe authored and GitHub committed 2026-10-22 10:55:55 +08:00
1 parent 752f1ed63d
commit eb3b1e33d2
2 files changed
+5

No files matched your search

+3
View File
@@ -712,6 +712,9 @@ func IntSlice(slice any) []int {
// Play: https://go.dev/play/p/800B1dPBYyd // Play: https://go.dev/play/p/800B1dPBYyd
func DeleteAt[T any](slice []T, index int) []T { func DeleteAt[T any](slice []T, index int) []T {
result := append([]T(nil), slice...) result := append([]T(nil), slice...)
if len(result) == 0 {
return result
}
if index < 0 || index >= len(slice) { if index < 0 || index >= len(slice) {
return result[:len(slice)-1] return result[:len(slice)-1]
+2
View File
@@ -801,6 +801,8 @@ func TestDeleteAt(t *testing.T) {
t.Parallel() t.Parallel()
assert := internal.NewAssert(t, "TestDeleteAt") assert := internal.NewAssert(t, "TestDeleteAt")
assert.Equal(0, len(DeleteAt([]int(nil), 0)))
assert.Equal(0, len(DeleteAt([]int{}, -1)))
tests := []struct { tests := []struct {
slice []int slice []int