What is Type int64 in Golang?

Golang int64 is a 64-bit signed integer ranging from -9223372036854775808 to 9223372036854775807

The int64 is a version of int that only stores signed numeric values composed of up to 64 bits.

Types of Ints in Go

Go has the following type of integers.

  1. int8
  2. int16
  3. int32
  4. int64
  5. uint8
  6. uint16
  7. uint32
  8. uint64

Integers are platform-dependent because if you are on a 32-bit system, it will hold a signed int32 variable; if you are working on a 64-bit system, it will have a signed int64 variable. The 8, 16, 32, and 64 tell us how many bits each type uses.

The uint means “unsigned integer”, while an int means “signed integer”. Unsigned integers only contain positive numbers (or zero).

In this tutorial, we will explicitly talk about the int64 type.

How to create an int64 variable in Go

To create an int64 variable, use the var var_name int64 syntax.

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var num int64 = 10
  fmt.Print(num, "\n")
  fmt.Print(reflect.TypeOf(num), "\n")
}

Output

10
int64

In this example, we created an int64 variable and printed its value and data type using the reflect.TypeOf() function.

While converting, you need to ensure that you don’t have a type that has a range lower than your current range; otherwise, you will lose your data.

Golang Integer Overflow

The integer overflow error in Golang may occur if you assign a type and then use a number larger than the type range to assign it.

package main

import (
  "fmt"
  "reflect"
)

func main() {
  var num int8 = 1000
  fmt.Print(num, "\n")
  fmt.Print(reflect.TypeOf(num), "\n")
}

Output

cannot use 1000 (untyped int constant) as int8 value in variable declaration (overflows)

To fix the overflows error in Go, continually assign a type and use an integer compatible with that type.

That’s it.