Stage 1 · Code
Variables, Types & Control Flow
Constants & Zero Values
Use const and iota for values that never change, and let Go's zero values give every variable a safe starting point.
Constants with const
A constant is a value that cannot change after it is defined. Constants are evaluated at compile time, so they have no runtime overhead. Use them for named values that are meaningful throughout your code — status codes, version strings, mathematical limits.
A var at package level is initialized once but can be changed by any code with access to it. A const is truly immutable. Prefer const for values that should never change; it makes your program easier to reason about and slightly more efficient.
iota for Enumerations
iota is a special identifier inside a const block that starts at 0 and increments by one for each constant in the block. It is Go's compact way to write sequential integer constants — enumerations.
Untyped Constants
When you write a constant without an explicit type, Go gives it a default kind but no concrete type. This lets the same constant be used with different typed expressions without an explicit conversion.
Untyped constants are more flexible. Use a typed constant (e.g. const MaxConnections int32 = 100) only when the type itself carries meaning — for example when passing to an API that requires a specific integer width.
Zero Values
When you declare a variable without an initial value in Go, it is automatically set to its zero value. This means Go never leaves memory uninitialised. There is no concept of an undefined variable at runtime.
| Type | Zero value | Meaning |
|---|---|---|
| int, int8…int64, uint… | 0 | Numerically zero |
| float32, float64 | 0.0 | Numerically zero |
| string | "" | Empty string |
| bool | false | Logically false |
| pointer | nil | No memory address |
| slice | nil | Empty, length and capacity zero |
| map | nil | Nil map (reading returns zero; writing panics) |
| interface | nil | No concrete type or value |
| struct | all fields zeroed | Each field is its own zero value |
Languages that leave variables uninitialised produce unpredictable bugs when code accidentally reads before writing. Go's zero values mean a var count int is always 0, a var flag bool is always false, and a var s Server is always a safely-typed struct — no surprises.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.