Golang Max Int: The Largest Possible Value for an Int

The maximum value for an int type in the Golang on 64-bit platforms is “9223372036854775807”. The max value is defined by the size of a type specified by the specification. Therefore, the maximum int value depends on the int size on the platform where the Golang code is executing.

To get the maximum value of the int64 type in Golang, you can use the “math.MaxInt64” constant.

Two predeclared integer types with implementation-specific sizes exist in Golang:

  1. Unsigned integers have either 32 or 64 bits.
  2. A signed integer has the same size as an unsigned integer.

Example 1

package main

import (
  "fmt"
  "math"
)

func main() {
  fmt.Printf("int64 max: %d\n", math.MaxInt64)
}

Output

int64 max: 9223372036854775807

Example 2

If you are using 32-bit platforms, use the math.MaxInt32 constant.

package main

import (
  "fmt"
  "math"
)

func main() {
  fmt.Printf("int32 max: %d\n", math.MaxInt32)
}

Output

int32 max: 2147483647

Example 3

If you want maximum int value based on the platform you are executing, go code, and try the below code.

package main

import (
  "fmt"
)

func main() {
  maxInt := int(^uint(0) >> 1)
  fmt.Println(maxInt)
}

Output

9223372036854775807

You can see that I am using Macbook M1 64-bit OS. That’s why it returns the max int for 64-bit platforms.

That’s pretty much it.

Leave a Comment