Go 1.21 brings the two built-in functions min and max to compute the smallest or largest value of a fixed number of arguments of ordered types.

min max example

Only a fixed number of arguments is supported. Go needs to know the number of arguments at compile time.

min max error

For string arguments the result for min is the first argument with the smallest (or for max, largest) value, compared lexically byte-wise.

min max shadow

These new built-ins will not break your existing code that already uses the names min and max. Built-ins aren’t keywords, so min and max can be shadowed however you like.

min max shadow