Files
lo/docs/data/it-lastorempty.md
T
2025-10-08 19:35:53 +02:00

1.3 KiB

name, slug, sourceRef, category, subCategory, signatures, playUrl, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures playUrl variantHelpers similarHelpers position
LastOrEmpty lastorempty it/find.go#L400 it find
func LastOrEmpty[T any](collection iter.Seq[T]) T
https://go.dev/play/p/9QkDH3-zp
it#find#lastorempty
core#slice#lastorempty
560

Returns the last element of a collection or zero value if empty.

Will iterate through the entire sequence.

Examples:

// Get the last element or zero value
numbers := it.Slice([]int{5, 2, 8, 1, 9})
last := it.LastOrEmpty(numbers)
// last: 9

// With empty collection
empty := it.Slice([]int{})
last := it.LastOrEmpty(empty)
// last: 0 (zero value for int)

// With strings
words := it.Slice([]string{"hello", "world", "go"})
last := it.LastOrEmpty(words)
// last: "go"

emptyWords := it.Slice([]string{})
last := it.LastOrEmpty(emptyWords)
// last: "" (zero value for string)

// With structs
type Person struct {
    Name string
    Age  int
}
people := it.Slice([]Person{
    {Name: "Alice", Age: 30},
    {Name: "Bob", Age: 25},
})
last := it.LastOrEmpty(people)
// last: {Name: "Bob", Age: 25}

emptyPeople := it.Slice([]Person{})
last := it.LastOrEmpty(emptyPeople)
// last: {Name: "", Age: 0} (zero value for Person)

// With single element
single := it.Slice([]int{42})
last := it.LastOrEmpty(single)
// last: 42