From 4be99b6e2aa560b6ca2f1c439439c1ad15eddea8 Mon Sep 17 00:00:00 2001 From: Kei Kamikawa Date: Tue, 5 Nov 2024 09:17:35 +0900 Subject: [PATCH] fixed FindValueByIndex logic --- internal/sliceutil/sliceutil.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/internal/sliceutil/sliceutil.go b/internal/sliceutil/sliceutil.go index 0ebddcf..05c37d4 100644 --- a/internal/sliceutil/sliceutil.go +++ b/internal/sliceutil/sliceutil.go @@ -1,12 +1,10 @@ package sliceutil // FindValueByIndex returns the value of the index in s, -// or -1 if not present. +// or zero value if not present. func FindValueByIndex[S ~[]E, E any](s S, idx int) (v E) { - for i := range s { - if i == idx { - return s[i] - } + if idx < 0 || idx >= len(s) { + return v // return zero value of type E } - return v + return s[idx] }