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 ad740ac
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions internal/sliceutil/sliceutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package sliceutil
// FindValueByIndex returns the value of the index in s,
// or -1 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 ad740ac

Please sign in to comment.