Skip to main content
Help improve this documentation

This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.

Your feedback helps us improve!

Core - Time helpersโ€‹

This page lists all time operations available in the core package of lo.

  • Measures execution time of a function. Variants return the elapsed duration alongside 0 to 10 returned values from the function.

    // Base variant (no return values): Duration
    elapsedOnly := lo.Duration(func() {
    time.Sleep(3 * time.Millisecond)
    })
    _ = elapsedOnly

    // Zero-return variant: Duration0
    elapsed := lo.Duration0(func() {
    time.Sleep(10 * time.Millisecond)
    })
    _ = elapsed

    // One-return variant: Duration1
    v, dur := lo.Duration1(func() int {
    time.Sleep(5 * time.Millisecond)
    return 123
    })
    _ = v
    _ = dur

    // Two-return variant: Duration2
    a, b, elapsed2 := lo.Duration2(func() (int, string) {
    time.Sleep(2 * time.Millisecond)
    return 7, "x"
    })
    _ = a
    _ = b
    _ = elapsed2
    Variant:
    Prototypes:
    func Duration(callback func()) time.Duration
    func Duration0(callback func()) time.Duration
    func Duration1[T any](callback func() T) (T, time.Duration)
    func Duration2[T, U any](callback func() (T, U)) (T, U, time.Duration)
    func Duration3[T, U, V any](callback func() (T, U, V)) (T, U, V, time.Duration)
    func Duration4[T, U, V, W any](callback func() (T, U, V, W)) (T, U, V, W, time.Duration)
    func Duration5[T, U, V, W, X any](callback func() (T, U, V, W, X)) (T, U, V, W, X, time.Duration)
    func Duration6[T, U, V, W, X, Y any](callback func() (T, U, V, W, X, Y)) (T, U, V, W, X, Y, time.Duration)
    func Duration7[T, U, V, W, X, Y, Z any](callback func() (T, U, V, W, X, Y, Z)) (T, U, V, W, X, Y, Z, time.Duration)
    func Duration8[T, U, V, W, X, Y, Z, A any](callback func() (T, U, V, W, X, Y, Z, A)) (T, U, V, W, X, Y, Z, A, time.Duration)
    func Duration9[T, U, V, W, X, Y, Z, A, B any](callback func() (T, U, V, W, X, Y, Z, A, B)) (T, U, V, W, X, Y, Z, A, B, time.Duration)
    func Duration10[T, U, V, W, X, Y, Z, A, B, C any](callback func() (T, U, V, W, X, Y, Z, A, B, C)) (T, U, V, W, X, Y, Z, A, B, C, time.Duration)