Go Integer Data Types

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).


1️⃣ Signed Integer Types

Signed integers can store positive and negative values.

Type Size Range
int 32 or 64 bit Platform dependent
int8 8 bit −128 to 127
int16 16 bit −32,768 to 32,767
int32 32 bit −2,147,483,648 to 2,147,483,647
int64 64 bit −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
var a int = 100
var b int32 = -500
var c int64 = 1000000000

2️⃣ Unsigned Integer Types

Unsigned integers store only non-negative values.

Type Size Range
uint 32 or 64 bit Platform dependent
uint8 8 bit 0 to 255
uint16 16 bit 0 to 65,535
uint32 32 bit 0 to 4,294,967,295
uint64 64 bit 0 to 18,446,744,073,709,551,615
var x uint = 50
var y uint8 = 255

3️⃣ Special Integer Aliases

Alias Actual Type Use
byte uint8 Binary data, bytes
rune int32 Unicode characters
var ch rune = 'A'
var b byte = 255

4️⃣ Default Integer Type

If no type is specified, Go uses int.

num := 10 // int

✔ Best for general-purpose integer operations


5️⃣ Integer Operations

a := 20
b := 6

fmt.Println(a + b) // Addition
fmt.Println(a - b) // Subtraction
fmt.Println(a * b) // Multiplication
fmt.Println(a / b) // Division
fmt.Println(a % b) // Modulus


6️⃣ Integer Overflow

Go does not automatically warn about overflow.

var x uint8 = 255
x = x + 1 // overflow → 0

⚠ Be careful with small integer types.


7️⃣ Type Conversion with Integers

❌ Not allowed:

var a int = 10
var b int64 = a // error

✅ Allowed:

var b int64 = int64(a)

8️⃣ Checking Integer Type

fmt.Printf("%T\n", a)

9️⃣ 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...