underscore/docs/content/collections/first.md
Ruidy 3617c2de8f
docs: update Last documentation to reflect panic behavior
Document that Last panics on empty slices with a clear error message.
Add examples for single element and empty slice cases.

Related to Issue 13 (PR #41)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-16 08:51:36 +01:00

505 B

title date
First 2025-01-16T00:00:00-00:00

First returns the first element of the slice. Returns an error if the slice is empty.

package main

import (
 "fmt"
 u "github.com/rjNemo/underscore"
)

func main() {
 nums := []int{1, 2, 3, 4, 5}
 first, err := u.First(nums)
 if err != nil {
  panic(err)
 }
 fmt.Println(first) // 1

 // Handle empty slice
 empty := []int{}
 _, err = u.First(empty)
 if err != nil {
  fmt.Println("Error:", err) // Error: underscore: empty slice
 }
}