Goroutines are lightweight functions that the Go runtime runs concurrently with the rest of your program. You start one by putting the go keyword in front of a function call, and the runtime schedules it across OS threads for you. Because goroutines start with a stack of only a few kilobytes, a single program can run hundreds of thousands of them.
Starting a goroutine is the easy part. The real work is knowing when it finishes, how it shares data safely and how it stops. This guide covers each of those with current Go idioms (Go 1.22 and later).
A goroutine is a function executing independently of its caller. The syntax is a normal call prefixed with go:
The call returns immediately and the caller keeps running. Arguments are evaluated at the moment of the go statement, and any return value of the function is discarded. Anonymous functions work too:
Goroutines are not OS threads. The Go scheduler multiplexes many goroutines onto a smaller pool of threads, sized by default to the number of CPU cores (GOMAXPROCS). When a goroutine blocks on a channel, a lock or network I/O, the scheduler runs another one on the same thread.
Many tutorials keep main alive with time.Sleep. That is a guess, not synchronisation. When main returns, the program exits and every running goroutine is killed, whatever it was doing.
Use a primitive that waits for completion instead: sync.WaitGroup, a channel, or errgroup.
sync.WaitGroup counts running goroutines. Call Add before starting each one, Done when it finishes and Wait to block until the count reaches zero. Since Go 1.25 there is also wg.Go(func()), which does the Add and Done for you.
The output order of the workers is not fixed, but all workers finished always prints last. Since Go 1.22 each loop iteration has its own copy of i, so the closure above captures the right value. In older versions you had to pass i as an argument or shadow it with i := i.
Channels let goroutines pass values to each other and synchronise at the same time. A send on an unbuffered channel blocks until another goroutine receives.
The Go proverb applies here: share memory by communicating, do not communicate by sharing memory. Use channels to hand off ownership of data, and close a channel from the sending side when no more values will arrive so receivers can range over it.
When several goroutines must touch the same variable, guard it with sync.Mutex, or use sync/atomic for simple counters. Without protection you have a data race and the result is undefined.
Run your tests with go test -race ./... or go run -race . to detect races. The race detector reports the exact lines of conflicting reads and writes.
A goroutine that never exits is a leak. The standard way to tell it to stop is context.Context:
Create the context with context.WithCancel or context.WithTimeout and call the cancel function when the work is no longer needed. For a group of goroutines that can fail, golang.org/x/sync/errgroup runs them, collects the first error and cancels the shared context for you.
No. Goroutines are managed by the Go runtime, start with a small growable stack and are scheduled onto OS threads. Creating one is much cheaper than creating a thread.
Not by itself. Concurrency is structuring a program as independent tasks. Tasks run in parallel only when the runtime has more than one CPU thread available, which GOMAXPROCS controls.
Send it on a channel, or write it to a slice index or variable that only that goroutine touches and read it after Wait returns. The return value of the function itself is discarded.
Hundreds of thousands are practical because of the small stack. The limit is usually memory and what the goroutines do, so cap concurrent work with a worker pool or errgroup.SetLimit when it touches databases or networks.
Start concurrent work with the go keyword, wait for it with sync.WaitGroup or channels, never with time.Sleep, and protect shared data with a mutex or atomics. Give every goroutine a way to stop through context, and test with the race detector to catch mistakes early.