Back to Lo

Core Latestby

docs/data/core-latestby.md

1.53.0327 B
Original Source

Searches a collection for the element with the maximum time extracted by the predicate. Returns zero value when the collection is empty.

go
type Event struct{ At time.Time }
events := []Event{{At: time.Now()}, {At: time.Now().Add(2 * time.Hour)}}
last := lo.LatestBy(events, func(e Event) time.Time {
    return e.At
})