From 147e486753d93bdf5c557c2cc0aa5ad8d7de9023 Mon Sep 17 00:00:00 2001 From: weiihann Date: Thu, 16 Jan 2025 16:23:15 +0800 Subject: [PATCH] fix Unique() on nil slice --- utils/slices.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/utils/slices.go b/utils/slices.go index 47d2f7f40..a6da9d5dd 100644 --- a/utils/slices.go +++ b/utils/slices.go @@ -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))