John Hoffman
John Hoffman

Reputation: 18617

min for comparing time.Durations?

Comparing 2 time.Duration values in go with math.Min errs :

cannot use someTime (type time.Duration) as type float64 in argument to math.Min

I could use an if else statement to get the min duration, but is there a native min function for getting the min duration?

Upvotes: 8

Views: 13568

Answers (1)

Paul Hankin
Paul Hankin

Reputation: 58241

There's nothing in the standard library, but it's simple to write yourself.

func minDuration(a, b time.Duration) time.Duration {
    if a <= b { return a }
    return b
}

[update 2023]

You can use the builtin min since go release 1.21. Now it's as simple as min(a, b) where a and b are time.Duration values.

Upvotes: 13

Related Questions