By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Prefix a call with go and it runs concurrently: go process(job). The goroutine starts with a tiny stack that grows as needed, and the Go runtime multiplexes many goroutines onto a smaller number of operating-system threads. You are not managing threads; you are deciding which calls may overlap.
The scheduler runs on GOMAXPROCS logical processors — by default the number of CPUs. A goroutine yields at function calls, channel operations and blocking system calls, and since Go 1.14 the runtime can also preempt long-running loops. That means a tight loop cannot starve the scheduler forever, but it still should not exist: do the work in chunks or check a context.
Concurrency helps when work is independent and at least partly waiting — I/O, HTTP calls, independent CPU-bound batches. It does not make a single slow computation faster on one core, and it does not protect shared state. Those are separate concerns: the scheduler decides when goroutines run; you decide how much of them run and how they communicate.
Three rules prevent most goroutine bugs. Do not start a goroutine you cannot wait for or cancel. Do not read the goroutine's results through unsynchronized memory. Do not let main return while background work is still running — when main returns, the process exits and silently drops every goroutine.
package main
import (
"fmt"
"slices"
"sync"
)
func main() {
statuses := []int{200, 404, 500, 503, 201}
results := make([]string, len(statuses))
var wg sync.WaitGroup
for index, status := range statuses {
wg.Add(1)
go func() {
defer wg.Done()
if status >= 500 {
results[index] = fmt.Sprintf("%d failure", status)
return
}
results[index] = fmt.Sprintf("%d success", status)
}()
}
wg.Wait()
slices.Sort(results)
for _, line := range results {
fmt.Println(line)
}
}sync.WaitGroup is the simplest join primitive: Add before starting the goroutine, Done inside it, Wait when you must know they all finished. (defer wg.Done() is the pattern.) The mistake to avoid is calling Add inside the goroutine — the wait may then race ahead of the add. A sync.WaitGroup must also never be copied after first use.
| Goroutine vs OS thread | Goroutine | Typical OS thread |
|---|---|---|
| Initial stack | A few KiB, grows on demand | 1-8 MiB fixed reservation |
| Creation cost | Nanoseconds to microseconds | Microseconds to milliseconds |
| Scheduling | Go runtime, cooperative and preemptive | Kernel scheduler |
| Practical count | Hundreds of thousands | Thousands |
| Communication | Channels and shared memory | Locks and OS primitives |
Key takeaways
go f() runs a call concurrently on a runtime-managed goroutine with a growable stack.
The scheduler multiplexes goroutines over GOMAXPROCS OS threads and preempts long loops.
Concurrency helps independent or I/O-bound work; it does not protect shared state.
sync.WaitGroup joins goroutines — call Add before the go statement.
When main returns, the process exits and unfinished goroutines are lost.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.