underscore
GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Last

Last returns the last element of the slice. Panics if the slice is empty with a clear error message.

package main

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

func main() {
	nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5}
	fmt.Println(u.Last(nums)) // 5

	// Single element
	single := []int{42}
	fmt.Println(u.Last(single)) // 42

	// Empty slice panics with clear message
	// empty := []int{}
	// u.Last(empty) // panic: underscore.Last: empty slice
}