Goroutine — lightweight concurrency primitive managed by Go runtime. Thread — OS-level execution unit with dedicated kernel resources.
Key differences:
Implementation:
1// Goroutine — cheap and easy2go func() {3 fmt.Println("Running concurrently")4}()56// Multiple goroutines for fan-out pattern7var wg sync.WaitGroup8for i := 0; i < 1000; i++ {9 wg.Add(1)10 go func(id int) {11 defer wg.Done()12 processItem(id)13 }(i)14}15wg.Wait()
Scheduling behavior:
When to use goroutines:
When to use threads:
Common mistakes:
sync.WaitGroup or channels).runtime.LockOSThread() without understanding the performance implications.