Skip to content

Commit 2651a33

Browse files
rjNemoclaude
andauthored
fix: add explicit panic for Last on empty slice (#41)
- Add length check with explicit panic message - Update documentation to note panic behavior - Tests already exist and pass Resolves Issue 13 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent d622c8c commit 2651a33

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

last.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package underscore
22

3-
// Last returns the last element of the slice
3+
// Last returns the last element of the slice.
4+
// Panics if the slice is empty.
45
func Last[T any](values []T) T {
5-
n := len(values)
6-
return values[n-1]
6+
if len(values) == 0 {
7+
panic("underscore.Last: empty slice")
8+
}
9+
return values[len(values)-1]
710
}

0 commit comments

Comments
 (0)