Golang max function
last modified May 8, 2025
This tutorial explains how to use the max
built-in function in Go.
We'll cover basic usage with practical examples of finding maximum values.
The max function is used to find the largest value among its arguments. It was introduced in Go 1.21 as part of the new built-in functions for comparisons.
In Go, max
works with ordered types like integers, floats, and
strings. It returns the maximum value from the provided arguments, which must be
of the same type.
Basic max with integers
The simplest use of max
compares two integer values. This example
demonstrates basic integer comparison.
Note: All arguments must be of the same comparable type.
package main import "fmt" func main() { a := 42 b := 27 maximum := max(a, b) fmt.Printf("The max of %d and %d is %d\n", a, b, maximum) // Can compare more than two values c := 99 fmt.Printf("The max of %d, %d, and %d is %d\n", a, b, c, max(a, b, c)) }
The function returns the largest integer from the provided arguments. It works with two or more values of the same integer type.
Using max with floating-point numbers
The max
function works equally well with floating-point numbers.
This example shows floating-point comparisons with max.
package main import "fmt" func main() { x := 3.14 y := 2.71 z := 1.618 fmt.Printf("Max of %.2f and %.2f is %.2f\n", x, y, max(x, y)) fmt.Printf("Max of %.2f, %.2f, and %.2f is %.2f\n", x, y, z, max(x, y, z)) // Works with negative numbers fmt.Println("Max of -10.5 and -20.3:", max(-10.5, -20.3)) }
The function correctly handles floating-point comparisons, including negative numbers. It maintains full floating-point precision in the result.
Finding max string value
The max
function can compare strings lexicographically. This example
shows string comparisons using max.
package main import "fmt" func main() { s1 := "apple" s2 := "banana" s3 := "cherry" fmt.Println("Max string:", max(s1, s2)) fmt.Println("Max of three strings:", max(s1, s2, s3)) // Case-sensitive comparison fmt.Println("Max with different cases:", max("Go", "go", "GO")) }
String comparison is case-sensitive and follows Unicode lexicographical order. The function returns the string that would appear last in a sorted list.
Using max with custom ordered types
The max
function works with any type that supports ordering.
This example demonstrates using max with custom types.
package main import "fmt" type Temperature float64 func main() { t1 := Temperature(23.5) t2 := Temperature(19.2) t3 := Temperature(25.7) hottest := max(t1, t2, t3) fmt.Printf("The highest temperature is %.1f°C\n", hottest) // Also works with other custom ordered types type Day int const ( Monday Day = iota Tuesday Wednesday ) fmt.Println("Latest weekday:", max(Monday, Tuesday, Wednesday)) }
The function works with any type that implements ordered comparisons. Custom types must be based on built-in ordered types to work with max.
Edge cases with max function
This example explores edge cases and special behaviors of the max function. It demonstrates handling of NaN values and empty arguments.
package main import ( "fmt" "math" ) func main() { // With NaN values nan := math.NaN() val := 42.0 fmt.Println("Max with NaN:", max(nan, val)) fmt.Println("Max with NaN first:", max(val, nan)) // Requires at least one argument // This would cause compile-time error: // fmt.Println(max()) // invalid operation: max() (no arguments) // Single argument returns itself fmt.Println("Max of single value:", max(7)) }
When NaN values are present, max always returns NaN. The function requires at least one argument and returns it unchanged when only one is provided.
Source
This tutorial covered the max
function in Go with practical
examples of finding maximum values across different data types.
Author
List all Golang tutorials.