Returns the maximum value of two or more numbers.
Use math.Inf(-1)
to set the initial maximum value to negative infinity.
Use range
and math.Max()
to iterate over the numbers and get the maximum value.
代码实现
import "math"
func MaxOf(nums ...float64) float64 {
max := math.Inf(-1)
for _, num := range nums {
max = math.Max(num, max)
}
return max
}
使用样例
MaxOf(3.0, 4.0, 2.0) // 4.0