feat: slice 包新增 GetValue 和 GetValueHandle 函数,用于获取特定索引的元素,如果索引超出范围将返回零值

This commit is contained in:
kercylan98 2023-08-21 15:02:15 +08:00
parent 93e63b1ace
commit 2dd5dd5c6c
1 changed files with 15 additions and 0 deletions

View File

@ -5,6 +5,21 @@ import (
"reflect" "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 删除特定索引的元素 // Del 删除特定索引的元素
func Del[V any](slice *[]V, index int) { func Del[V any](slice *[]V, index int) {
s := *slice s := *slice