https://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli=1

The germane part:

Since integer types use two's complement arithmetic, you can infer the min/max constant values for int and uint. For example,

const MaxUint = ^uint(0) 
const MinUint = 0 
const MaxInt = int(MaxUint >> 1) 
const MinInt = -MaxInt - 1

As per @CarelZA's comment:

uint8  : 0 to 255 
uint16 : 0 to 65535 
uint32 : 0 to 4294967295 
uint64 : 0 to 18446744073709551615 
int8   : -128 to 127 
int16  : -32768 to 32767 
int32  : -2147483648 to 2147483647 
int64  : -9223372036854775808 to 9223372036854775807
Answer from nmichaels on Stack Overflow
🌐
Go Packages
pkg.go.dev › math
math package - math - Go Packages
Floor returns the greatest integer value less than or equal to x.
Discussions

Writing a function that returns the max value of a type [~float64 | ~uint | ~int T]
I would just use reflection here. var maxes = [...]any{ reflect.Int: math.MaxInt, reflect.Uint8: math.MaxUint8, reflect.Uint64: uint64(math.MaxUint64), reflect.Float64: math.MaxFloat64, } func Max[T cmp.Ordered]() T { typ := reflect.TypeFor[T]() v := maxes[typ.Kind()] val := reflect.ValueOf(v).Convert(typ) return val.Interface().(T) } func main() { type myInt uint8 fmt.Println(Max[int]()) fmt.Println(Max[float64]()) fmt.Println(Max[uint64]()) fmt.Println(Max[myInt]()) } More on reddit.com
🌐 r/golang
11
23
March 17, 2024
math: add MaxUint, MinInt, MaxInt
The math package is lacking MaxInt, MaxUint, MinInt and MinUint. This makes writing programs that assign a max-value to int or uint without manually specifying the value impossible, if you want to ... More on github.com
🌐 github.com
14
November 1, 2018
🌐
Leapcell
leapcell.io › blog › understanding-maximum-integer-values-in-go
Understanding Maximum Integer Values in Go | Leapcell
July 25, 2025 - By selecting appropriate types and implementing proper checks, you can prevent and handle integer overflows effectively. Use math.MaxInt, which provides the platform-specific maximum int value.
🌐
gosamples
gosamples.dev › tutorials › the maximum and minimum value of the int types in go
🍰 The maximum and minimum value of the int types in Go
April 14, 2022 - int min - max: -9223372036854775808 - 9223372036854775807 int8 min - max: -128 - 127 int16 min - max: -32768 - 32767 int32 min - max: -2147483648 - 2147483647 int64 min - max: -9223372036854775808 - 9223372036854775807 uint min - max: 0 - 18446744073709551615 uint8 min - max: 0 - 255 uint16 ...
🌐
Google Groups
groups.google.com › g › golang-nuts › c › a9PitPAHSSU › m › ziQw1-QHw3EJ
[go-nuts] MAX/MIN NUMBER of each integer type
On Apr 16, 12:26 pm, peterGo <go.peter...@gmail.com> wrote: > Daniel > > On Apr 16, 12:12 pm, Ostsol <ost...@gmail.com> wrote: > > > int64, for > > example, has a maximum of 2^32 - 1. > > MaxInt64 = 1<<63 - 1 = 2^64 - 1 > > Peter ... Either email addresses are anonymous for this group or you ...
🌐
YourBasic
yourbasic.org › golang › max-min-int-uint
Maximum value of an int · YourBasic Go
yourbasic.org/golang · Go has two predeclared integer types with implementation-specific sizes: a uint (unsigned integer) has either 32 or 64 bits, an int (signed integer) has the same size as a uint. This code computes the limit values as untyped constants. const UintSize = 32 << (^uint(0) ...
Find elsewhere
🌐
DEV Community
dev.to › sw360cab › built-in-min-and-max-methods-in-go-121-24ph
Built-in min() and max() methods in Go 1.21 - DEV Community
April 24, 2024 - Once I would have used the math.Min method, which requires two float64 types as parameters, and I would have done the following: const MAX_TITLE_LENGTH int = 50 // a placeholder title var title string = "..." // the given title titleMaxLength ...
🌐
Rotational
rotational.io › blog › ranges-of-integer-data-types
Rotational Labs | Ranges of Integer Data Types
As a result, I find myself constantly checking the sizes of standard int types but I haven’t found a good way to Google this. This blog post is a quick reference for the standard sizes and a discussion on why it matters. So without further ado, here are the ranges of the standard numeric types: ... // Constants defined in the math package const ( MaxInt8 int8 = 1<<7 - 1 MinInt8 int8 = -1 << 7 MaxInt16 int16 = 1<<15 - 1 MinInt16 int16 = -1 << 15 MaxInt32 int32 = 1<<31 - 1 MinInt32 int32 = -1 << 31 MaxInt64 int64 = 1<<63 - 1 MinInt64 int64 = -1 << 63 MaxUint8 uint8 = 1<<8 - 1 MaxUint16 uint16 = 1<<16 - 1 MaxUint32 uint32 = 1<<32 - 1 MaxUint64 uint64 = 1<<64 - 1 ) // Complement method (system specific) const ( MaxUint uint = ^uint(0) MinUint uint = 0 MaxInt int = int(MaxUint >> 1) MinInt int = -MaxInt - 1 )
🌐
YourBasic
yourbasic.org › golang › max-min-function
Compute max of two ints/floats · YourBasic Go
yourbasic.org/golang · There is no built-in max or min function for integers, but it’s simple to write your own. // Max returns the larger of x or y. func Max(x, y int64) int64 { if x < y { return y } return x } // Min returns the smaller ...
🌐
TutorialsPoint
tutorialspoint.com › finding-maximum-of-two-numbers-in-golang
Finding Maximum of Two Numbers in Golang
April 12, 2023 - In the above code, we declare a function named max that takes two integer arguments a and b. Inside the function, we declare a variable named max and assign the value of a to it. Then, we use a shorthand if-else statement to check if b is greater than a. If it is, we assign the value of b to the max variable. Finally, we return the maximum value. Golang's built-in sort package provides a Ints function that takes a slice of integers as an argument and sorts it in ascending order.
🌐
Reddit
reddit.com › r/golang › writing a function that returns the max value of a type [~float64 | ~uint | ~int t]
r/golang on Reddit: Writing a function that returns the max value of a type [~float64 | ~uint | ~int T]
March 17, 2024 -

Today I was trying to write a generic function to return the maximum value for floats, ints and uints. It seems straightforward, right? It's not!

The function signature:

type Types interface {
    ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uint |
   	 ~int | ~int64 | ~int32 | ~int16 | ~int8 |
   	 ~float64 | ~float32
}

func infFor[T Types]() T

My first thought was to write a switch checking the type, like this:

func infFor[T Types]() T {
	var v T
	switch any(v).T {
	case float64:
    	return T(math.Inf(1))
	case int8:
    	return T(math.MaxInt8)
	...
	}
}

But this doesn't work for user defined types (type myInt int).

Then I tried to split the function in two parts, check if T is a float or (u)int, this makes the job easier.

func infFor[T Types]() T {
    // Check if T is a float.
    var f float64 = 1.5
    if float64(T(f)) == f {
   	 return T(math.Inf(1))
    }
    
	// Handle (u)ints ...
}

Converting 1.5 to an integer type will truncate the value and then float64(T(f)) == f is false.

The value 1.5 is important because it can be represented both by float64 and float32, 1.1 can't.

After the check we know the value is an integer, but the compiler doesn't, so we can't use bit arithmetic. The solution I found is to check when the value overflows.

This is the final version:

func infFor[T Types]() T {
    // Check if T is a float.
    var f float64 = 1.5
    if float64(T(f)) == f {
   	 return T(math.Inf(1))
    }

    maxValues := [...]uint64{
   	 math.MaxInt8,
   	 math.MaxUint8,
   	 math.MaxInt16,
   	 math.MaxUint16,
   	 math.MaxInt32,
   	 math.MaxUint32,
   	 math.MaxInt64,
   	 math.MaxUint64,
    }

    var v T
    // Check when v overflows.
    for i := 0; v+1 > 0; i++ {
   	 v = T(maxValues[i])
    }

    return v
}
🌐
PixelsTech
pixelstech.net › home › articles › why no max/min function for integer in golang
Why no max/min function for integer in GoLang | PixelsTech
June 8, 2019 - GoLang intentionally omits built-in max/min functions for integer types, a common feature in other languages. While math.Max and math.Min exist for float64, this is due to the inherent complexity of floating-point comparisons, which Go provides ...
🌐
Dot Net Perls
dotnetperls.com › int-max-go
Go - int Max (math.MaxInt) - Dot Net Perls
for i := range math.MaxInt32 { if i % 100_000_000 == ... INT MIN: -9223372036854775808 MAX: 9223372036854775807 BYTE MAX: 255 UINT MAX: 18446744073709551615 INT32 MIN: -2147483648 MAX: 2147483647 0 100000000 200000000 300000000 400000000 500000000 600000000 700000000 800000000 900000000 1000000000 ...
🌐
GitHub
github.com › golang › go › issues › 28538
math: add MaxUint, MinInt, MaxInt · Issue #28538 · golang/go
November 1, 2018 - The math package is lacking MaxInt, MaxUint, MinInt and MinUint. This makes writing programs that assign a max-value to int or uint without manually specifying the value impossible, if you want to ...
Author   golang
🌐
GeeksforGeeks
geeksforgeeks.org › go language › finding-maximum-of-two-numbers-in-golang
Finding Maximum of Two Numbers in Golang - GeeksforGeeks
April 28, 2025 - // Golang program to illustrate // how to find the largest number package main import ( "fmt" "math" ) // Main function func main() { // Finding largest number // among the given numbers // Using Max() function nvalue_1 := math.Max(34, 67) nvalue_2 := math.Max(56.7, 90.8) // Adding maximum numbers res := nvalue_1 + nvalue_2 fmt.Printf("%.2f + %.2f = %.2f", nvalue_1, nvalue_2, res) } Output: 67.00 + 90.80 = 157.80 · Comment · Article Tags: Article Tags: Go Language · Golang-Math · Basics · Go Programming Language (Introduction)7 min read ·
🌐
IncludeHelp
includehelp.com › golang › math-maxint-constant-with-examples.aspx
Golang math.MaxInt Constant with Examples
August 26, 2021 - The return type of math.MaxInt constant is int, it returns the highest (maximum) value that can be represented by an int. // Golang program to demonstrate the // example of math.MaxInt Constant package main import ( "fmt" "math" ) func main() { fmt.Printf("Type of math.MaxInt is %T\n", math.MaxInt) ...
🌐
Google Groups
groups.google.com › g › golang-nuts › c › f32UN1TYiAI
integer max/min/abs etc in the math package
For int, one implementation is literally: ... I wrote that in less time than I would've spent starting up a browser, doing a search for 'golang Abs', and noticing that math.Abs only deals in float64, and my version certainly does a lot less redundant processing to arrive at the correct answer (and on amd64 deployments, actually _can_ arrive at the correct answer for all possible values).