Golang Max Int: The Largest Possible Value for an Int

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.

In this tutorial, we will find the max int value on the platform on which you use the Go language.

Golang max int

The maximum value for an int type in the Golang is defined by the size of a type specified by the specification.

The maximum value of int depends on the size of int on the platform where the Golang code is executing.

To get the maximum value of the int64 type, use math.MaxInt64 constant.

package main

import (
  "fmt"
  "math"
)

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

Output

int64 max: 9223372036854775807

On 64-bit platforms, the integer is normally 64 bits, so the maximum value of int is 9223372036854775807.

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

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