From 2dd5dd5c6c96809dc8623863be6dba8adaabe25c Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Mon, 21 Aug 2023 15:02:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20slice=20=E5=8C=85=E6=96=B0=E5=A2=9E=20G?= =?UTF-8?q?etValue=20=E5=92=8C=20GetValueHandle=20=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E8=8E=B7=E5=8F=96=E7=89=B9=E5=AE=9A?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E7=9A=84=E5=85=83=E7=B4=A0=EF=BC=8C=E5=A6=82?= =?UTF-8?q?=E6=9E=9C=E7=B4=A2=E5=BC=95=E8=B6=85=E5=87=BA=E8=8C=83=E5=9B=B4?= =?UTF-8?q?=E5=B0=86=E8=BF=94=E5=9B=9E=E9=9B=B6=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/slice/slice.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/utils/slice/slice.go b/utils/slice/slice.go index 70d6e12..ce52da2 100644 --- a/utils/slice/slice.go +++ b/utils/slice/slice.go @@ -5,6 +5,21 @@ import ( "reflect" ) +// GetValue 获取特定索引的元素,如果索引超出范围则返回零值 +func GetValue[V any](slice []V, i int) (v V) { + if i >= 0 && i < len(slice) { + return slice[i] + } + return +} + +// GetValueHandle 获取特定索引的元素,并通过 handle 进入传入,如果索引超出范围则不会进入 handle +func GetValueHandle[V any](slice []V, i int, handle func(v V)) { + if i >= 0 && i < len(slice) { + handle(slice[i]) + } +} + // Del 删除特定索引的元素 func Del[V any](slice *[]V, index int) { s := *slice