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
go.dev › play › p › qeElniC-Nin
Go Playground - The Go Programming Language
Any requests for content removal should be directed to security@golang.org.
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
Using minimum and maximum int64 limits leads to error
Problem statement A tool I am using is generating the following schema, which sets minimum and maximum limits for a parameter of type integer: "schema": { "maximum": 92233720368... More on github.com
🌐 github.com
8
April 11, 2022
go - Get maximum int64 value using 2's complement arithmetic golang - Stack Overflow
How to get maximum int64 value using 2's complement arithmetic in golang More on stackoverflow.com
🌐 stackoverflow.com
proposal: math: MinOf and MaxOf for generic numeric limits
Currently when writing a function involving a generic numeric type, there's no easy way to determine the maximum or minimum possible value of that type, because the math.Min* and math.Max* cons... More on github.com
🌐 github.com
14
December 7, 2021
🌐
Rotational
rotational.io › blog › ranges-of-integer-data-types
Rotational Labs | Ranges of Integer Data 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 ...
🌐
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 - To get the maximum and minimum value of various integer types in Go, use the math package constants. For example, to get the minimum value of the int64 type, which is -9223372036854775808, use the math.MinInt64 constant. To get the maximum value of the int64 type, which is 9223372036854775807, ...
🌐
Leapcell
leapcell.io › blog › understanding-maximum-integer-values-in-go
Understanding Maximum Integer Values in Go | Leapcell
July 25, 2025 - The maximum value of int in Go depends on the system architecture (32-bit or 64-bit).
🌐
YourBasic
yourbasic.org › golang › max-min-int-uint
Maximum value of an int · YourBasic Go
const UintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64 const ( MaxInt = 1&lt;<(UintSize-1) - 1 // 1<<31 - 1 or 1<<63 - 1 MinInt = -MaxInt - 1 // -1 << 31 or -1 << 63 MaxUint = 1<<UintSize - 1 // 1<<32 - 1 or 1<<64 - 1 ) The UintSize constant is also available in package math/bits. ... Go blueprints: code for com­mon tasks is a collection of handy code examples. Pick the right one: int vs. int64
🌐
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
}
🌐
GitHub
github.com › go-swagger › go-swagger › issues › 2755
Using minimum and maximum int64 limits leads to error · Issue #2755 · go-swagger/go-swagger
April 11, 2022 - int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807) ... if err := validate.MinimumInt("limit", "query", *o.Limit, -9.223372036854776e+18, false); err != nil { return err } if err := validate.MaximumInt("limit", ...
Author   avdv
Find elsewhere
🌐
Go Packages
pkg.go.dev › builtin
builtin package - builtin - Go Packages
The max built-in function returns the largest value of a fixed number of arguments of cmp.Ordered types. There must be at least one argument.
🌐
Educative
educative.io › answers › what-is-type-int64-in-golang
What is type int64 in Golang?
The following code shows the positive limit of the values that an int64 variable can store and what happens if you try to store something larger than the stipulated range of values: package main · import "fmt" func main() { // initializing with the maximum allowed positive number ·
🌐
Google Groups
groups.google.com › g › golang-nuts › c › a9PitPAHSSU › m › ziQw1-QHw3EJ
[go-nuts] MAX/MIN NUMBER of each integer type
to golang-nuts · 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 ·  · unread, Apr 16, 2010, 12:47:31 PM4/16/10 ·  ·  ·  · Reply to author ·
🌐
ZetCode
zetcode.com › golang › builtins-int64-type
Understanding the int64 Type in Golang
May 8, 2025 - The int64 type represents signed 64-bit integers in Go. It can store values from -263 to 263-1.
🌐
Jamessimas
jamessimas.com › posts › golang-max-and-minimum-int-values
https://www.jamessimas.com/posts/2024/golang-max-and-minimum-int-values/
March 5, 2024 - nil maps in Golang 2023-01-07 go · Thoughts on travel to Hawaii 2022-11-08 travel
🌐
PixelsTech
pixelstech.net › home › articles › time to think about supporting max/min functions for integers in golang
Time to think about supporting max/min functions for integers in GoLang | PixelsTech
July 24, 2021 - package main import ( "fmt" ) type ... if a > b { return a } return b } func main() { fmt.Println(min(45, 74)) fmt.Println(max(4.141, 2.01)) } GoLang does support a new constraint for data type as any which means any ty...
🌐
GitHub
github.com › golang › go › issues › 50019
proposal: math: MinOf and MaxOf for generic numeric limits · Issue #50019 · golang/go
December 7, 2021 - // MinOf returns the minimum possible value of type T. func MinOf[T contraints.Integer | constraints.Float]() T // MaxOf returns the maximum possible value of type T. func MaxOf[T contraints.Integer | constraints.Float]() T · It's currently not possible to write the above functions without using reflection until #45380 is fixed, but here's an approximation: https://gotipplay.golang.org/p/0i3Wc7i4dJs ·
Author   rogpeppe
🌐
Medium
kirillshevch.medium.com › how-to-find-min-or-max-value-in-a-stack-with-go-c45c6bc695f8
How to find Min or Max value in a Stack with Go | by Kirill Shevchenko | Medium
March 27, 2022 - The next step is to write a construct function to create the new one instance of the stack. We just have to set min to be a maximum int64.
🌐
YourBasic
yourbasic.org › golang › int-vs-int64
Pick the right one: int vs. int64 · YourBasic Go
func Max(a []int64) int64 { max := a[0] for i := 1; i < len(a); i++ { if max < a[i] { max = a[i] } } return max }