To get the max value of integer type and min value of int type in golang:
- Use and import the math package
- math package has constants for integer limit values.
- Use math.MaxInt64 to get the max int64 value which is 9223372036854775807
- math constants for max and min values are available for different integer types like MaxInt, MinInt, MaxInt8, MinInt8, MaxInt16, MinInt16, MaxInt32, MinInt32, MaxInt64, MinInt64 etc…
Refer to the following table to get maximum and minimum values for the integer types in golang.
Constant | Value | Integer Value |
MaxInt | 1<<(intSize-1) – 1 | 9223372036854775807 |
MinInt | -1 << (intSize – 1) | -9223372036854775808 |
MaxInt8 | 1<<7 – 1 | 127 |
MinInt8 | -1 << 7 | -128 |
MaxInt16 | 1<<15 – 1 | 32767 |
MinInt16 | -1 << 15 | -32768 |
MaxInt32 | 1<<31 – 1 | 2147483647 |
MinInt32 | -1 << 31 | -2147483648 |
MaxInt64 | 1<<63 – 1 | 9223372036854775807 |
MinInt64 | -1 << 63 | -9223372036854775808 |
MaxUint | 1<<intSize – 1 | 18446744073709551615 |
MaxUint8 | 1<<8 – 1 | 255 |
MaxUint16 | 1<<16 – 1 | 65535 |
MaxUint32 | 1<<32 – 1 | 4294967295 |
MaxUint64 | 1<<64 – 1 | 18446744073709551615 |
In this article, we will discuss max int type values in golang, min int values, and how to get math constants min, and maximum integer values in the golang program.
How to Get Maximum and Minimum Values of Integer Types in Golang
To get the max and min values of int types in golang, refer to the following golang program.
package main import ( "fmt" "math" ) func main() { // MaxInt fmt.Printf("MaxInt max value: %d\n", math.MaxInt) // MinInt fmt.Printf("MinInt min value: %d\n", math.MinInt) // MaxInt8 fmt.Printf("MaxInt8 max value: %d\n", math.MaxInt8) // MinInt8 fmt.Printf("MinInt8 min value: %d\n", math.MinInt8) // MaxInt16 fmt.Printf("MaxInt16 max value: %d\n", math.MaxInt16) // MinInt16 fmt.Printf("MinInt16 min value: %d\n", math.MinInt16) // MaxInt32 fmt.Printf("MaxInt32 max value: %d\n", math.MaxInt32) // MinInt32 fmt.Printf("MinInt32 min value: %d\n", math.MinInt32) // MaxInt64 fmt.Printf("MaxInt64 max value: %d\n", math.MaxInt64) // MinInt64 fmt.Printf("MinInt64 min value: %d\n", math.MinInt64) // uint fmt.Printf("MaxUint max value: %d\n", uint(math.MaxUint)) // MaxUint8 fmt.Printf("MaxUint8 max value: %d\n", math.MaxUint8) // MaxUint16 fmt.Printf("Maxint16 max value: %d\n", math.MaxUint16) // MaxUint32 fmt.Printf("MaxUint32 max value: %d\n", math.MaxUint32) // MaxUint64 fmt.Printf("MaxUint64 max value: %d\n", uint64(math.MaxUint64)) }
Output
MaxInt max value: 9223372036854775807
MinInt min value: -9223372036854775808
MaxInt8 max value: 127
MinInt8 min value: -128
MaxInt16 max value: 32767
MinInt16 min value: -32768
MaxInt32 max value: 2147483647
MinInt32 min value: -2147483648
MaxInt64 max value: 9223372036854775807
MinInt64 min value: -9223372036854775808
MaxUint max value: 18446744073709551615
MaxUint8 max value: 255
Maxint16 max value: 65535
MaxUint32 max value: 4294967295
MaxUint64 max value: 18446744073709551615
In the above golang program, we import the math package and use the mathematical constant to get maximum and minimum int types values in go.
Conclusion
I hope the above article helped you to understand about max int type value in golang and minimum integer type values as well using the golang program.
You can find more topics about the Golang tutorials on the GolangSpot Home page.