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] }