on
Find the Min or Max Value in a Slice (New to Go 1.21)
The latest Go release, 1.21, has introduced two new builtin functions: min()
and max()
. These functions have long been used in many other languages, such as Python and JavaScript, and are now welcome additions to Go.
The min() and max() functions have been introduced to simplify the process of finding the smallest and largest numbers in a set, respectively. There was plenty of talk as to whether these should be included as “builtins” or introduced into the cmp package.
How to use min() and max()
Note: This will only work on fixed data sets (not slices)
This method uses builtins, see below if you’re working with slices.


Working with Slices?
The slices
[docs] package also contains a way of finding the min and max values within our data set, as seen below. We have a simple slice with the numbers 1 to 3 and then finding both the min and max on that.

