Technical Article => Programming => Go

You may notice that there is no max/min function provided to compare the maximum/minimum of two or more integers if you are a GoLang developer with some experience . In other languages, these functions are provided as part of the core lib functions. Have you wondered why?

Indeed GoLang provides max/min function in math package, but they are used for comparing float64 data type. The signature of these two functions are

math.Min(float64, float64) float64 math.Max(float64, float64) float64

The reason why GoLang provides the max/min function for comparing float64 numbers are that floating numbers are very complicated to handle for most of developers, it's not that straightforward to compare two float numbers given the nature of computer architecture. To avoid the trouble brought to the application developed if developers implement their own logic of max/min for float numbers, they are provided by GoLang math package as built in functions.

As for int/int64 data types, the max/min logic is relative easy to implement. A developer with basic skill can implement these two functions easily for integer types.

func Min(x, y int64) int64 { if x < y { return x } return y } func Max(x, y int64) int64 { if x > y { return x } return y }

In addition, to keep GoLang as concise and clean as possible, there is no support of generics in GoLang. Since there is max/min implemented for float64 data types, there cannot be function with the same name but with different parameter type implemented in the same math package. So below two cannot exist at the same package.

math.Max(float64, float64) float64 math.Max(int64, int64) int64

This design consideration conforms to the design principle of GoLang, to make GoLang as concise and clean as possible.