Intermediate 14 min readModule: Module 2: Basic Syntax, Variables & Constants
Variables, Short Declaration (:=) & Zero Values
Declare variables with short assignment (:=), understand default zero values (0, '', nil), and use iota for enums.
What You Will Learn in This Lesson
- Short variable declaration (name := 'Alex') inside functions
- Go's guaranteed zero values (0 for numbers, false for booleans, nil for pointers)
- Auto-incrementing enum constants with iota
Introduction & Core Concept
Go is statically typed with strong type inference. Uninitialized variables in Go are automatically set to their zero value, preventing uninitialized memory bugs.
WHY DOES THIS MATTER IN THE REAL WORLD?
Zero values eliminate null pointer bugs in basic variables, ensuring predictable initialization across all types.
Short Declarations & iota Constants
gogo
1234567891011121314package mainimport "fmt"const (RoleStudent = iota // 0RoleTeacher // 1RoleAdmin // 2)func main() {name := "Alex Dev"var count int // Zero value: 0fmt.Printf("User: %s | Count: %d | Role: %d\n", name, count, RoleAdmin)}
Line-by-Line Technical Breakdown
1Constants must be resolvable at compile time.
Try It Yourself (Interactive Editor)
Modify the code in real-time and click Run to test live browser output and console logs.
Intelligent Code Runner & Live Sandbox[GO]
GO SOURCE EDITOR
Interactive Live CodeIndustry Best Practices & Professional Standards
- Use := for local variables; use explicit var for package-level declarations.
Lesson Summary & Core Takeaways
- Guaranteed zero values make Go initialization predictable and safe.