Skip to content

Commit

Permalink
fixed FindValueByIndex logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex committed Nov 5, 2024
1 parent 1eafd18 commit 4be99b6
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions internal/sliceutil/sliceutil.go
Original file line number Diff line number Diff line change
@@ -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]
}

0 comments on commit 4be99b6

Please sign in to comment.