Basic Types
Explore Go numeric primitives, booleans, immutable strings, bytes, and Unicode runes.
โTypes are like specific container shapes: a water cup, a shoebox, and a letter envelope. You can not pour soup into an envelope โ Go enforces this strictly to prevent runtime crashes.โ
[String: "Go ไธ็"] โโโ Bytes: [71, 111, 32, 228, 184, 150, 231, 149, 140] (9 bytes total) โโโ Runes: ['G', 'o', ' ', 'ไธ', '็'] (5 Unicode characters)
Key Concepts & Rules To Remember
- Integers: `int`, `int8`, `int16`, `int32`, `int64` and unsigned `uint` variants.
- Floating points: `float32` and `float64` (default for decimals).
- Strings: Immutable sequences of UTF-8 bytes.
- `byte` is an alias for `uint8` (raw 8-bit byte), `rune` is an alias for `int32` (Unicode character code point).
1. Integers and Floats
In Go, `int` is architecture-dependent (64-bit on 64-bit systems). Type conversion is ALWAYS explicit โ Go never silently converts `int` to `float64`.
var x int = 42
var y float64 = float64(x) // Explicit conversion required!2. Bytes vs Runes
Because Go natively supports UTF-8, English letters take 1 byte, while emojis and international characters take 2โ4 bytes. A `rune` represents a single full character.
s := "Hi ๐"
fmt.Println(len(s)) // 7 bytes (H:1, i:1, space:1, rocket:4)
// Range iterates by RUNES (characters), not raw bytes:
for index, char := range s {
fmt.Printf("Char %c at byte index %d\n", char, index)
}Common Beginner Pitfalls & Mistakes
Knowledge Check
Verify your understanding with these interactive practice questions.
Quizzes
Quick checks for understanding
Multiple-choice with inline explanationsโexpand to see why.
Finished this lesson?
Mark it as complete to track your overall Go mastery.