Chapter 17: Integer

Integer data types.

In Go, integers are very explicit — unlike Python (one int type) or JavaScript (everything becomes Number), Go gives you precise control over size and signedness. This is intentional: Go was designed for systems programming, performance-critical code, protocols, embedded systems, crypto → you often need to know exactly how many bits you’re using.

Let’s go through everything like we’re sitting together with laptop open — theory, table, declaration patterns, real examples, common traps, and when to choose which type.

1. Two Big Families of Integers in Go

Family Can be negative? Types available Zero value Most common in 2025–2026 code
Signed Yes int, int8, int16, int32, int64 0 int (general purpose)
Unsigned No (≥ 0 only) uint, uint8, uint16, uint32, uint64, uintptr 0 uint (sizes, indices), uint8 (byte)

2. Detailed Table — All Integer Types (2026 reality on 64-bit systems)

Type Bits Signed? Min value Max value Typical use case Recommendation / Notes
int 32 or 64 (usually 64 now) Yes –2⁶³ or –2³¹ 2⁶³–1 or 2³¹–1 General counters, lengths, loop indices Default choice for most code
int8 8 Yes –128 127 Small signed values, old hardware, packed data Rare unless memory tight
int16 16 Yes –32,768 32,767 Audio samples, some network protocols Uncommon today
int32 32 Yes –2,147,483,648 2,147,483,647 Unicode code points (rune alias) Use when you need exactly 32 bits
int64 64 Yes –9.22×10¹⁸ 9.22×10¹⁸ Timestamps (UnixNano), large IDs, crypto Very common for big numbers
uint 32 or 64 (usually 64) No 0 2⁶⁴–1 or 2³²–1 Array lengths, buffer sizes, bit fields Use when value cannot be negative
uint8 8 No 0 255 byte alias — raw bytes, ASCII, colors Extremely common
uint16 16 No 0 65,535 Ports, RGB565 colors, some file formats Occasional
uint32 32 No 0 4,294,967,295 IPv4 addresses, CRC32, hashes Common in networking
uint64 64 No 0 18,446,744,073,709,551,615 File sizes, nanosecond timestamps, nonces Very common
uintptr same as uint (platform) No 0 platform max Low-level pointer arithmetic (rare) Almost never in normal code

3. Declaration & Literal Examples (Copy-Paste & Run)

Go

4. Important Rules & Behaviors

  • Literals without suffix are untyped until assigned → they can fit into any integer type that can hold them
Go
  • No implicit conversion between signed ↔ unsigned or different sizes
Go
  • Overflow is wrap-around (two’s complement) — no panic (unlike some languages)
Go

5. Common Real-World Patterns (2026 style)

Go

6. Your Quick Practice Exercise

Create integers.go and try:

Go

Change values, try to assign negative to uint → see compile error.

Questions now?

  • When exactly to prefer int64 over int?
  • How to safely convert between signed/unsigned?
  • Bitwise operations on integers?
  • Or next: floating-point types? strings? or composite types again?

Keep typing and running these — integers are the workhorse of almost every program. You’re doing great! 💪🇮🇳 Let’s keep going! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *