mirror of
https://github.com/samber/lo.git
synced 2026-04-22 15:37:14 +08:00
fa095e4b4f
* fix(doc): fix go playground demo URL * fix(doc): add more go playground demo URL
1.4 KiB
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 |
|
|
|
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")