trying_out_go/cmd/playground/variadic.go

34 lines
659 B
Go
Raw Normal View History

2023-11-04 13:08:46 -04:00
package main
import (
"fmt"
"math"
)
func maximum(numbers ...float64) float64 {
2024-02-03 11:15:06 -05:00
maxNumber := math.Inf(-1)
2023-11-04 13:08:46 -04:00
for _, number := range numbers {
2024-02-03 11:15:06 -05:00
if number > maxNumber {
maxNumber = number
2023-11-04 13:08:46 -04:00
}
}
2024-02-03 11:15:06 -05:00
return maxNumber
2023-11-04 13:08:46 -04:00
}
func inRange(min float64, max float64, numbers ...float64) []float64 {
var result []float64
for _, number := range numbers {
if number >= min && number <= max {
result = append(result, number)
}
}
return result
}
func variadic() {
fmt.Println(maximum(71.8, 56.2, 89.5))
fmt.Println(maximum(90.7, 89.7, 98.5, 92.3))
fmt.Println(inRange(1, 100, -12.5, 3.2, 0, 50, 103.5))
fmt.Println(inRange(-10, 10, 4.1, 12, -12, -5.2))
}