Go Integer Data Types

Go Tutorial

Go (Golang) – Integer Data Types

Go provides signed and unsigned integer data types with different sizes. Integer types are used to store whole numbers (without decimals).

 Signed Integer Types

Signed integers can store positive and negative values.

TypeSizeRange
int32 or 64 bitPlatform dependent
int88 bit−128 to 127
int1616 bit−32,768 to 32,767
int3232 bit−2,147,483,648 to 2,147,483,647
int6464 bit−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Unsigned Integer Types

Unsigned integers store only non-negative values.

TypeSizeRange
uint32 or 64 bitPlatform dependent
uint88 bit0 to 255
uint1616 bit0 to 65,535
uint3232 bit0 to 4,294,967,295
uint6464 bit0 to 18,446,744,073,709,551,615

 Special Integer Aliases

AliasActual TypeUse
byteuint8Binary data, bytes
runeint32Unicode characters

 Default Integer Type

If no type is specified, Go uses int.

  •  Best for general-purpose integer operations

 Integer Operations


 


 Integer Overflow

Go does not automatically warn about overflow.

  • Be careful with small integer types.

Type Conversion with Integers

 Not allowed:

Allowed:


Checking Integer Type


Best Practices

  •  Use int for most cases
  •  Use int64 for large numbers (DB, timestamps)
  •  Use uint only when negative values are impossible
  •  Avoid unnecessary small integer types

Summary

  • Go supports multiple integer sizes

  • Signed & unsigned types available

  • Default integer type is int

  • No implicit type conversion

  • Careful with overflow

You may also like...