比较时间持续时间的最小值是什么?

huangapple go评论76阅读模式
英文:

min for comparing time.Durations?

问题

在Go语言中,使用math.Min比较两个time.Duration值会出错:

无法将someTime(类型为time.Duration)作为参数中的float64类型传递给math.Min

我可以使用if else语句来获取最小的持续时间,但是否有原生的最小持续时间函数可用?

英文:

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?

答案1

得分: 10

这是要翻译的内容:

标准库中没有相关内容,但是你可以自己编写一个简单的函数。

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

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

func minDuration(a, b time.Duration) time.Duration {
    if a &lt;= b { return a }
    return b
}

huangapple
  • 本文由 发表于 2015年12月16日 08:06:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/34301784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定