Coding/Learn/go/go variables and data types
Go Foundational Course
01.Introduction, Toolchain & Package main
02.Variables, Zero Values, Constants & iota
03.Control Flow: If with Init Statements & Switch
04.The Unified For Loop & Range Iteration
05.Functions, Multiple Return Values & Named Returns
06.Arrays vs Slices: Dynamic Memory & Headers
07.Maps: Key-Value Hash Tables & Safe Access
08.Pointers & Value vs Pointer Semantics
09.Structs, Receiver Methods & Composition
10.Implicit Interfaces & Duck Typing with Typesafety
11.Explicit Error Handling, Wrapping & Inspection
12.Defer, Panic & Recover: Resource Cleanup
13.Strings, Runes & UTF-8 Encoding Internals
14.Goroutines & The M:N Work-Stealing Scheduler
15.Channels: CSP Concurrency & Pipelines
16.Select Statement, Non-Blocking IO & Timeouts
17.Mutexes, RWMutex & Atomic Primitives
18.Context Propagation, Cancellation & Deadlines
19.JSON Serialization, Tags & net/http Routing
20.Capstone: High-Performance Concurrent Microservice Engine
Course Certificate0%

Complete all lessons to earn

LearnVariables, Zero Values, Constants & iota
2 / 20 0%
Lesson 02 of 20
6 mins read

Variables, Zero Values, Constants & iota

PreviousNext Lesson
In Plain English

Go offers explicit variable declarations with var and type-inferred short declarations with := inside functions. Uninitialized variables automatically receive their zero value (0, "", false, nil). The iota enumerator simplifies successive integer constant definitions.

The Mental Model

"In Go, uninitialized variables are never filled with random memory garbage: declaring a variable is like receiving a freshly cleaned whiteboard erased to 0, empty string, or false."

Deep Dive: How It Works

Short Declaration (:=): Infers type automatically at compile time (only valid inside function bodies).

Guaranteed Zero Values: int/float = 0, string = "", bool = false, pointers/slices/maps = nil.

iota Constant Generator: Automatically increments by 1 for each line in a const () block.

Syntax Blueprint

var total int = 10
count := 42 // Inferred int
const (
    StatusPending = iota // 0
    StatusActive         // 1
)

Declare explicit vars, short inferred variables, and auto-incrementing constants with iota.

PointCore Rules to Remember

Point star
Zero Memory Safety: Variables are never left uninitialized, preventing catastrophic memory leak read bugs.
Point star
Strict Type System: No implicit type conversion: int32 and int64 cannot be added without explicit cast int64(x).
Point star
iota Constants: Provides clean, type-safe enumerated state constants without manual number assignment.

Common Beginner Traps & How to Fix Them

Mistake:Using short declaration := at package global scope outside of functions.

Why it happens: := is only valid inside function bodies.

How to fix: Use var name = value or var name Type at package level.

Pro Tip: Group related package constants inside a single const () block for readability and documentation.

Live Interactive Example

Hit Run Code to see it live
Typed Constants and iota States
Go 1.22 / Runtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Output Console
Click "Run Code" to view the rendered output.
How it works: StateRunning evaluates to 1 via iota, and requestCount is zero-initialized to 0.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Inspect StateStopped iota Code

  • PointPrint "Stopped State: %d\n" using the StateStopped constant with fmt.Printf.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates