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, use the math.MaxInt64
. To check the minimum and maximum values of different int types, see the following example and its output.
For unsigned integer types, only the max constant is available because the minimum value of unsigned types is always 0.
package main
import (
"fmt"
"math"
)
func main() {
// int
fmt.Printf("int min - max: %d - %d\n", math.MinInt, math.MaxInt)
// int8
fmt.Printf("int8 min - max: %d - %d\n", math.MinInt8, math.MaxInt8)
// int16
fmt.Printf("int16 min - max: %d - %d\n", math.MinInt16, math.MaxInt16)
// int32
fmt.Printf("int32 min - max: %d - %d\n", math.MinInt32, math.MaxInt32)
// int64
fmt.Printf("int64 min - max: %d - %d\n", math.MinInt64, math.MaxInt64)
// unsigned
// uint
fmt.Printf("uint min - max: %d - %d\n", uint(0), uint(math.MaxUint))
// uint8
fmt.Printf("uint8 min - max: %d - %d\n", 0, math.MaxUint8)
// uint16
fmt.Printf("uint16 min - max: %d - %d\n", 0, math.MaxUint16)
// uint32
fmt.Printf("uint32 min - max: %d - %d\n", 0, math.MaxUint32)
// uint64
fmt.Printf("uint64 min - max: %d - %d\n", 0, uint64(math.MaxUint64))
}
Output:
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 min - max: 0 - 65535
uint32 min - max: 0 - 4294967295
uint64 min - max: 0 - 18446744073709551615