Golang min function
last modified May 8, 2025
This tutorial explains how to use the min built-in function in Go.
We'll cover basic usage with practical examples of finding minimum values.
The min function returns the smallest value among its arguments. It was introduced in Go 1.21 as part of the new built-in functions for comparing ordered values. The function works with numeric types and strings.
In Go, min compares values of the same type and returns the
minimum. It can take two or more arguments. The function panics if called
with no arguments or with incompatible types.
Basic min with integers
The simplest use of min finds the smallest of two integers.
This example demonstrates basic integer comparison.
Note: All arguments must be of the same type.
package main
import "fmt"
func main() {
a := 42
b := 27
smallest := min(a, b)
fmt.Println("The smallest value is:", smallest)
// With more than two values
fmt.Println("Minimum of 5 values:", min(12, 8, 23, 5, 17))
}
The function returns 27 as it's smaller than 42. With multiple values, it finds the overall minimum (5 in the second case).
Min with floating-point numbers
The min function works with floating-point numbers similarly
to integers. This example shows floating-point comparison.
package main
import "fmt"
func main() {
x := 3.14
y := 2.71
z := 1.618
smallest := min(x, y, z)
fmt.Printf("The smallest float is: %.2f\n", smallest)
// Edge case with negative infinity
fmt.Println("Min with -Inf:", min(1.5, -1.0/0.0, 2.5))
}
The function correctly identifies 1.618 as the smallest value. It also handles special cases like negative infinity properly.
Min with strings
The min function can compare strings lexicographically.
This example demonstrates string comparison using min.
package main
import "fmt"
func main() {
s1 := "apple"
s2 := "banana"
s3 := "apricot"
first := min(s1, s2, s3)
fmt.Println("Lexicographically first:", first)
// Case sensitivity matters
fmt.Println("Min with mixed case:", min("Go", "go", "GO"))
}
The function returns "apple" as it's lexicographically smaller than "apricot" and "banana". Note that comparison is case-sensitive.
Min with custom types
For custom types, min requires the type to implement
the ordered constraints. This example shows min with type parameters.
package main
import (
"fmt"
"golang.org/x/exp/constraints"
)
type Temperature float64
func findMin[T constraints.Ordered](values ...T) T {
return min(values...)
}
func main() {
temps := []Temperature{22.5, 18.3, 25.0, 16.7}
minTemp := findMin(temps...)
fmt.Printf("Minimum temperature: %.1f°C\n", minTemp)
// Works with other ordered types
fmt.Println("Min duration:", findMin(3*time.Hour, 90*time.Minute))
}
The generic findMin function uses min internally.
It works with any type that satisfies the Ordered constraint.
Min in practical scenarios
This example shows practical use of min in a function that
calculates statistics while preventing division by zero.
package main
import (
"fmt"
"math"
)
func safeDivide(a, b float64) float64 {
minDenominator := min(math.Abs(b), 1.0)
if minDenominator == 0 {
return math.Inf(1)
}
return a / minDenominator
}
func main() {
fmt.Println("Safe division (5/0):", safeDivide(5, 0))
fmt.Println("Safe division (5/0.5):", safeDivide(5, 0.5))
fmt.Println("Safe division (5/-2):", safeDivide(5, -2))
// Another practical example
fmt.Println("Clamping value:", min(max(15, 10), 20))
}
Here min ensures the denominator is never smaller than 1.0 in
absolute value. This prevents division by very small numbers that could
cause floating-point overflow.
Source
This tutorial covered the min function in Go with practical
examples of finding minimum values across different types.
Author
List all Golang tutorials.