Cookies management by TermsFeed Cookie Consent

☯️ Min and max functions using Generics in Go

introduction generics generics-intro

Getting the minimum and maximum from a slice are some of the simplest functions that are written by developers during everyday coding. The problem is that when you wanted to get the minimum or maximum from int and float64 slices, up until now, you had to write two functions for each slice type. However, since version 1.18, Go has introduced the much-anticipated Generics, and you can now write single min() and max() functions that work for any ordered type.

This article is part of the Introduction to Go Generics series. Go here to see more.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main

import (
    "fmt"

    "golang.org/x/exp/constraints"
)

func max[T constraints.Ordered](s []T) T {
    if len(s) == 0 {
        var zero T
        return zero
    }
    m := s[0]
    for _, v := range s {
        if m < v {
            m = v
        }
    }
    return m
}

func min[T constraints.Ordered](s []T) T {
    if len(s) == 0 {
        var zero T
        return zero
    }
    m := s[0]
    for _, v := range s {
        if m > v {
            m = v
        }
    }
    return m
}

func main() {
    fmt.Println(min([]int{10, 2, 4, 1, 6, 8, 2}))
    fmt.Println(max([]float64{3.2, 5.1, 6.2, 7.6, 8.2, 1.5, 4.8}))
}

Generic functions need type parameters that describe types that are allowed for a given function and provide the type label used in the function body. As you can see in the example, they are declared in square brackets after the name of a function:

func name[TypeLabel Constraints](...) {
    ...
}

In the min() and max() functions we declare a type parameter T with constraints.Ordered constraint. It guarantees that the functions work only for slice types that support the operators <, <=, >=, >. The rest of the functions are pretty straightforward. They take a slice of type T as an argument, return a zero value for a given type if the slice has size 0, and find the minimum or maximum of the slice in a loop. As a result, they return a single min or max value of type T.

From the example above, we get the following output:

1
8.2

It is really exciting how, thanks to Generics, it is possible to make things that were not possible before in Go. We are sure that this feature will improve the code quality of any Go project 😊.


Thank you for being on our site 😊. If you like our tutorials and examples, please consider supporting us with a cup of coffee and we'll turn it into more great Go examples.

Have a great day!

Buy Me A Coffee

🫘 Count the occurrences of an element in a slice in Go

Learn how to count elements in a slice that meet certain conditions
introduction generics generics-intro

🗑️ Remove duplicates from any slice using Generics in Go

Learn how to create a slice with unique values using Generics
introduction slice generics generics-intro

🔉 Reduce function using Generics in Go

Learn how to define a function to accumulate slice values using Generics
introduction generics generics-intro