Skip to content

Commit

Permalink
fix Unique() on nil slice
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Jan 16, 2025
1 parent e697545 commit 147e486
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions utils/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ func AnyOf[T comparable](e T, values ...T) bool {

// Unique returns a new slice with duplicates removed
func Unique[T comparable](slice []T) []T {
if len(slice) == 0 {
return slice
}

// do not support unique on pointer types, just return the slice as it is
if len(slice) > 0 {
elt := slice[0]
if reflect.TypeOf(elt).Kind() == reflect.Ptr {
return slice
}
if reflect.TypeOf(slice[0]).Kind() == reflect.Ptr {
return slice
}

result := make([]T, 0, len(slice))
Expand Down

0 comments on commit 147e486

Please sign in to comment.