Files
lo/docs/data/core-latestbyerr.md
T
Samuel Berthe fa095e4b4f fix(doc): fix go playground demo URL (#832)
* fix(doc): fix go playground demo URL

* fix(doc): add more go playground demo URL
2026-03-06 00:09:59 +01:00

1.4 KiB

name, slug, sourceRef, category, subCategory, playUrl, signatures, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory playUrl signatures variantHelpers similarHelpers position
LatestByErr latestbyerr find.go#L737 core find https://go.dev/play/p/WpBUptwnxuG
func LatestByErr[T any](collection []T, iteratee func(item T) (time.Time, error)) (T, error)
core#find#latestbyerr
core#find#latestby
core#find#latest
core#find#earliestby
core#find#earliestbyerr
core#find#earliest
core#find#maxby
core#find#minby
core#find#maxindexby
core#find#minindexby
core#find#findby
core#find#findkeyby
core#find#findduplicatesby
core#find#finduniquesby
251

Searches a collection for the element with the maximum time extracted by the predicate. Returns zero value when the collection is empty. Stops iteration immediately when an error is encountered.

type Event struct{ At time.Time }
events := []Event{{At: time.Now()}, {At: time.Now().Add(2 * time.Hour)}}
last, err := lo.LatestByErr(events, func(e Event) (time.Time, error) {
    return e.At, nil
})
// Event{At: ...}, nil
// Error case - stops on first error
type Event struct{ At time.Time }
events := []Event{{At: time.Now()}, {At: time.Time{}}, {At: time.Now().Add(2 * time.Hour)}}
_, err := lo.LatestByErr(events, func(e Event) (time.Time, error) {
    if e.At.IsZero() {
        return time.Time{}, fmt.Errorf("zero time not allowed")
    }
    return e.At, nil
})
// error("zero time not allowed")