From 2651a3331af78acb530c86e435a8c640ab59f52a Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 16 Nov 2025 08:44:10 +0100 Subject: [PATCH] fix: add explicit panic for Last on empty slice (#41) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- last.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/last.go b/last.go index d5703a8..a025a08 100644 --- a/last.go +++ b/last.go @@ -1,7 +1,10 @@ package underscore -// Last returns the last element of the slice +// Last returns the last element of the slice. +// Panics if the slice is empty. func Last[T any](values []T) T { - n := len(values) - return values[n-1] + if len(values) == 0 { + panic("underscore.Last: empty slice") + } + return values[len(values)-1] }