Goroutine — lightweight concurrency primitive managed by Go runtime. Thread — OS-level execution unit with dedicated kernel resources.
Stack allocation:
Scheduling model:
Performance characteristics:
1// Goroutine-based concurrent server2func handleConnections(ln net.Listener) {3 for {4 conn, err := ln.Accept()5 if err != nil {6 log.Println("accept error:", err)7 continue8 }9 go handleConnection(conn) // one goroutine per connection10 }11}
When to use goroutines:
When to use threads:
Common mistakes:
runtime.LockOSThread() unnecessarily.