Goroutine — lightweight, managed by Go runtime. Thread — OS-level execution unit managed by the operating system.
Memory footprint:
Scheduling:
Creation and teardown:
1// Spawning thousands of goroutines is trivial2for i := 0; i < 1000000; i++ {3 go func(id int) {4 // Each goroutine uses ~2KB initially5 runtime.Gosched() // yield to scheduler6 }(i)7}
When to use goroutines vs threads:
Common mistakes:
runtime.LockOSThread() without understanding the cost.sync.WaitGroup or channels to coordinate goroutine completion, leading to data races.