Go Integer Data Type

In Go, integer data types are used to represent numbers without decimal points, like 150, -72, or 100000.

Tutorials dojo strip



Integer data types are classified into two main categories:

  1. Signed Integers
    These integers can store both positive and negative values.
  2. Unsigned Integers
    These integers are restricted to storing only non-negative values.

Note: If you don’t specify an integer type explicitly, the default type is int.




Signed Integers

Signed integers are declared using one of the int keywords and can hold values that are either positive or negative.

Example: Declaring Signed Integers

Go

Output:

Go


Types of Signed Integers in Go

TypeSizeRange
intDepends on platform:-2147483648 to 2147483647 (32-bit systems)
32-bit or 64-bit-9223372036854775808 to 9223372036854775807 (64-bit systems)
int88 bits / 1 byte-128 to 127
int1616 bits / 2 bytes-32768 to 32767
int3232 bits / 4 bytes-2147483648 to 2147483647
int6464 bits / 8 bytes-9223372036854775808 to 9223372036854775807




Unsigned Integers

Unsigned integers are declared using one of the uint keywords and can store only non-negative values.

Example: Declaring Unsigned Integers

Go

Output:

Go


Types of Unsigned Integers in Go

TypeSizeRange
uintDepends on platform:0 to 4294967295 (32-bit systems)
32-bit or 64-bit0 to 18446744073709551615 (64-bit systems)
uint88 bits / 1 byte0 to 255
uint1616 bits / 2 bytes0 to 65535
uint3232 bits / 4 bytes0 to 4294967295
uint6464 bits / 8 bytes0 to 18446744073709551615




Choosing the Right Integer Type

When selecting an integer type, consider the range of values the variable will store.

Example of Incorrect Type Assignment

This example demonstrates an error caused by assigning a value beyond the range of int8 (valid range is -128 to 127):

Go

Result:

Go
Tutorials dojo strip