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
intanduint. 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 Top answer 1 of 13
350
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
intanduint. 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
2 of 13
99
I would use math package for getting the maximal and minimal values for integers:
package main
import (
"fmt"
"math"
)
func main() {
// integer max
fmt.Printf("max int64 = %+v\n", math.MaxInt64)
fmt.Printf("max int32 = %+v\n", math.MaxInt32)
fmt.Printf("max int16 = %+v\n", math.MaxInt16)
// integer min
fmt.Printf("min int64 = %+v\n", math.MinInt64)
fmt.Printf("min int32 = %+v\n", math.MinInt32)
fmt.Printf("max float64 = %+v\n", math.MaxFloat64)
fmt.Printf("max float32 = %+v\n", math.MaxFloat32)
// etc you can see more int the `math`package
}
Output:
max int64 = 9223372036854775807
max int32 = 2147483647
max int16 = 32767
min int64 = -9223372036854775808
min int32 = -2147483648
max float64 = 1.7976931348623157e+308
max float32 = 3.4028234663852886e+38
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.
Writing a function that returns the max value of a type [~float64 | ~uint | ~int T]
23 votes, 11 comments. Today I was trying to write a generic function to return the maximum value for floats, ints and uints. It seems… More on reddit.com
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
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
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
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, ...
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<<(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 common 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 - r/golang · • 2y ago · rschio · 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 ·
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
Go Packages
pkg.go.dev › builtin
builtin package - builtin - Go Packages
int64 is the set of all signed 64-bit integers.
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 ·
Stack Overflow
stackoverflow.com › questions › 77281192 › get-maximum-int64-value-using-2s-complement-arithmetic-golang
go - Get maximum int64 value using 2's complement arithmetic golang - Stack Overflow
Found: const MaxInt64 := int64(^uint64(0) >> 1) or const MaxInt64 = 1<<63 - 1, also const MaxUint = ^uint64(0)
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 Numeric interface { type int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64 } func min[T Numeric](a, b T) T { if a < b { return a } return b } func max[T Numeric](a, b T) T { 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 type.
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