Why no max/min function for integer in GoLang

  sonic0002        2019-06-08 07:00:55       58,044        12         

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.

INT  GOLANG  MAX  INT64 

           

  RELATED


  12 COMMENTS


Anonymous
Jun 9, 2019 at 6:43 am

it's not that straightforward to compare two float numbers given the nature of computer architecture

 

floating point equality is sometimes hard (you need some fuzziness sometimes), but checking greater than or less than is trivial -- compare the exponents, and if they are equal, compare the mantissas

Anonymous
Nov 29, 2019 at 12:18 pm

well, you forgot to check sign first, so your algorithm failed. But yes, for floats the same function as the suggested for ints above could have been used, as there are already less/more then binary comparative operators for floats in golang.

ElGallo
Aug 25, 2019 at 10:54 am

They really should just make a `MaxInt` function given that it is a common primitive, similar to how they have an `Ints` function in the `sort` package.

Anonymous
Jan 17, 2020 at 6:33 am

This is why I dislike golang. You guys just don't write golang code, aren't you!

Anonymous
Apr 5, 2020 at 9:24 pm

It is a problem, I have to write it so often...

Anonymous
Jul 19, 2020 at 12:31 pm

coming from Python3. implementing these basic stuff over and over again is a pain

Ke Pi
Jul 25, 2020 at 9:17 am

ya, that's why generic is needed for such cases.

Anonymous
Aug 12, 2020 at 5:46 am

"make GoLang as concise and clean as possible." - what a fanboy statement to make.

This omission is ridiculous.

Anonymous
Aug 14, 2020 at 9:10 pm

Luckily generic support is on the way

Anonymous
Apr 3, 2022 at 5:45 pm

Lol but generics are totally overload for such a feature, you could just overload the method for different types. This language is ridiculous in some aspects, I can't understand the decisions behind such design in the era of expressive syntaxes and rich standard library. This is really putting me off from an otherwise good company (Datadog), just because I can't imagine myself dealing with such stupid decisions.

Anonymous
Feb 2, 2022 at 11:58 pm

I want to use the language. I don't want to write these naive student exercise for  my work

Ke Pi
Feb 7, 2022 at 6:50 am

it soon has generic support, then we may have some library solving this kind of problems.



  PROGRAMMER HUMOR

Where is my bike?


  SUPPORT US