Skip to content

Commit 4be99b6

Browse files
committed
fixed FindValueByIndex logic
1 parent 1eafd18 commit 4be99b6

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

internal/sliceutil/sliceutil.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package sliceutil
22

33
// FindValueByIndex returns the value of the index in s,
4-
// or -1 if not present.
4+
// or zero value if not present.
55
func FindValueByIndex[S ~[]E, E any](s S, idx int) (v E) {
6-
for i := range s {
7-
if i == idx {
8-
return s[i]
9-
}
6+
if idx < 0 || idx >= len(s) {
7+
return v // return zero value of type E
108
}
11-
return v
9+
return s[idx]
1210
}

0 commit comments

Comments
 (0)