mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +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, signatures, playUrl, variantHelpers, similarHelpers, position
| name | slug | sourceRef | category | subCategory | signatures | playUrl | variantHelpers | similarHelpers | position | |||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Latest | latest | it/find.go#L346 | it | find |
|
https://go.dev/play/p/r5Yq6ATSHoH |
|
|
520 |
Searches for the latest (maximum) time.Time in a collection.
Returns zero value when the collection is empty. Will iterate through the entire sequence.
Examples:
import "time"
// Find the latest time from a collection
times := it.Slice([]time.Time{
time.Date(2023, 5, 15, 10, 0, 0, 0, time.UTC),
time.Date(2023, 3, 20, 14, 30, 0, 0, time.UTC),
time.Date(2023, 8, 1, 9, 15, 0, 0, time.UTC),
})
latest := it.Latest(times)
// latest: 2023-08-01 09:15:00 +0000 UTC
// With empty collection
empty := it.Slice([]time.Time{})
latest := it.Latest(empty)
// latest: 0001-01-01 00:00:00 +0000 UTC (zero value)
// Find latest from parsed times
times := it.Slice([]time.Time{
time.Parse(time.RFC3339, "2023-01-01T12:00:00Z"),
time.Parse(time.RFC3339, "2023-01-01T10:00:00Z"),
time.Parse(time.RFC3339, "2023-01-01T14:00:00Z"),
})
latest := it.Latest(times)
// latest: 2023-01-01 14:00:00 +0000 UTC
// Find latest log entry timestamp
logs := it.Slice([]time.Time{
time.Now().Add(-2 * time.Hour),
time.Now().Add(-1 * time.Hour),
time.Now(),
})
latest := it.Latest(logs)
// latest: current time