Golang / GoLang Concurrency Mastery Interview Questions
Why can Go run millions of goroutines while equivalent OS-thread workloads fail?
Three fundamental differences make goroutines dramatically cheaper than OS threads: initial stack size, scheduling cost, and blocking behaviour.
| Aspect | OS Thread | Goroutine |
|---|---|---|
| Initial stack | 1–8 MB (fixed, kernel-allocated) | 2 KB (grows dynamically up to 1 GB) |
| Scheduling | Preemptive by OS (expensive context switch with full register save) | Cooperative + async-preemptive by Go runtime (user-space, cheap) |
| Blocking | Blocks the entire OS thread on syscall | Parks goroutine; OS thread freed for other goroutines |
| Creation cost | ~10 µs, requires kernel call | ~300 ns, entirely user-space |
| Practical limit | ~10,000 before memory exhaustion | ~1,000,000+ on standard hardware |
package main
import (
"fmt"
"runtime"
"sync"
)
func main() {
const n = 1_000_000
var wg sync.WaitGroup
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
defer wg.Done()
// Each goroutine: ~2 KB stack at creation
// 1M goroutines ≈ 2 GB total — feasible on modern hardware
}()
}
wg.Wait()
fmt.Println("done, goroutines:", runtime.NumGoroutine())
}The small initial stack is possible because Go uses copy-on-grow stacks: when a goroutine needs more stack space the runtime allocates a new, larger stack, copies the old contents, updates all internal pointers, and frees the old stack. This is transparent to user code.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
