Back to Concepts Roadmap
🟤 Level 7 — Runtime & Concurrency
Concurrency
Goroutines (Lightweight Threads)
Asynchronous user-space threads multiplexed onto OS threads by the Go runtime.
Real-World Analogy (Mental Model)
“Instead of hiring a 10-ton industrial crane (OS Thread) for every small brick, Go creates thousands of origami paper workers (Goroutines) that start in 2 Kilobytes of memory.”
Interactive Goroutine & Channel Simulator
“Do not communicate by sharing memory; share memory by communicating.” Test how Go channels synchronize goroutines:
Channel:
ch := make(chan string, 2)Buffer: 0 / 2 itemsSender G1 (Worker)
Slot 1
Slot 2
Receiver G2 (Main)
Runtime Log: Channel is idle. Send a message to get started.
Key Concepts & Rules To Remember
- Spawned simply by adding `go` keyword before a function call: `go doTask()`.
- Initial stack is only 2 KB (grows and shrinks dynamically).
- A single Go process can effortlessly run hundreds of thousands of concurrent goroutines.
Step-by-Step Code
1. Understanding Goroutines (Lightweight Threads)
Asynchronous user-space threads multiplexed onto OS threads by the Go runtime. In Go, goroutines (lightweight threads) is designed around clarity and high runtime efficiency.
example.goGo 1.24+
func fetch(url string) {
fmt.Println("Fetched:", url)
}
func main() {
go fetch("https://example.com") // Runs in background!
time.Sleep(100 * time.Millisecond)
}Common Beginner Pitfalls & Mistakes
Mistake: Misusing goroutines (lightweight threads) without understanding its memory or concurrency semantics.
âś… Correct Way: Always follow standard Go idioms and verify with tests.
Self Assessment
Knowledge Check
Verify your understanding with these interactive practice questions.
Quizzes
Quick checks for understanding
Multiple-choice with inline explanations—expand to see why.
What is the primary concept behind Goroutines (Lightweight Threads)?
Finished this lesson?
Mark it as complete to track your overall Go mastery.