A goroutine is a lightweight concurrent function started with the go keyword. The Go runtime multiplexes goroutines onto OS threads—far cheaper than one thread per task in Java.
Starting goroutines
go func() {
fmt.Println("async")
}()
time.Sleep(time.Millisecond) // wait in playground demos
Main exits immediately without waiting—use channels, sync.WaitGroup, or time.Sleep in lessons so output appears before exit.
Compared to other models
- Node.js — single-threaded event loop; Go runs true parallel goroutines on multiple cores
- Python — GIL limits CPU parallelism in threads; Go goroutines scale for CPU and I/O
- Rust — async/threads with ownership; Go GC simplifies shared heap (with sync discipline)
Important interview questions and answers
- Q: Goroutine vs OS thread?
A: Goroutines are smaller, scheduled by Go runtime on a thread pool—millions are practical. - Q: What happens if main returns while goroutines run?
A: Program exits—unfinished goroutines are terminated.
Self-check
- What keyword starts a goroutine?
- Why use WaitGroup or channels instead of Sleep in production?
Tip: Main exits without waiting for goroutines—use channels, WaitGroup, or brief time.Sleep in playground demos so output appears.
Interview prep
- Goroutine vs thread?
Goroutines are lightweight, scheduled by Go runtime on OS thread pool—millions are practical.
- Main exits early?
Program terminates—unfinished goroutines are killed; use sync primitives to wait.