From d9aa8cbd4e288c35239a646b81afb3fc7487cb4f Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 31 Jan 2023 14:25:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/slice/slice.go | 6 ++++++ 1 file changed, 6 insertions(+) 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] }