Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

min for comparing time.Durations?

Tags:

go

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?

like image 908
John Hoffman Avatar asked May 17 '26 19:05

John Hoffman


1 Answers

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.

like image 196
Paul Hankin Avatar answered May 22 '26 21:05

Paul Hankin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!