diff --git a/helper/slice/slice.go b/helper/slice/slice.go index 0cb7366..0a74454 100644 --- a/helper/slice/slice.go +++ b/helper/slice/slice.go @@ -225,6 +225,12 @@ func Pop[T any](a *[]T) T { // Rand 随机取一个元素 func Rand[T any](a []T) (int, T) { + var r T + if len(a) < 1 { + return -1, r + } else if len(a) == 1 { + return 0, a[0] + } i := number.Rand(0, len(a)-1) return i, a[i] }