Goroutines and channels are the two building blocks of concurrency in Go. A goroutine is a function running independently, scheduled by the Go runtime rather than the operating system. A channel is a typed conduit that goroutines use to send values to each other and to synchronize, following the idea "share memory by communicating".
Together they let you run thousands of concurrent tasks and coordinate them without explicit locks in most cases.
You start a goroutine by putting go before a function call. Goroutines start with a small stack (a few kilobytes) that grows and shrinks on demand, and the runtime multiplexes them onto a small pool of OS threads, so creating tens of thousands is practical.
The important rule: when main returns, the program exits and all goroutines are killed. Never use time.Sleep to wait for them; use a sync.WaitGroup or a channel.
Since Go 1.22, each loop iteration has its own copy of i, so the closure above prints 1, 2 and 3 in some order. Before 1.22 you had to pass i as an argument to avoid every goroutine seeing the final value. In Go 1.25 and later you can also write wg.Go(func() { ... }), which combines Add, go and Done.
Create a channel with make. The arrow operator <- sends and receives; the arrow points in the direction data flows.
Because the channel is unbuffered, the send blocks until main is ready to receive, so the two goroutines meet at that point. This rendezvous is how channels double as synchronization.
| Channel | Created with | Send blocks when | Receive blocks when |
|---|---|---|---|
| Unbuffered | make(chan int) | No receiver is ready | No sender is ready |
| Buffered | make(chan int, 3) | The buffer is full | The buffer is empty |
Buffered channels decouple producers from consumers up to the buffer size, but a large buffer only hides a slow consumer; it does not fix it.
Rules worth memorizing:
v, ok := <-ch to detect closure.for v := range ch to receive until the channel is closed.The parameter type chan<- int is a send-only channel. Directional channel types document intent and let the compiler reject misuse.
A fixed number of workers read jobs from one channel and write results to another. The WaitGroup closes results only after every worker has finished, which avoids deadlock.
The order of results is not deterministic because workers run in parallel.
select blocks until one of its cases can proceed, which is how you add timeouts and cancellation:
Pass a context.Context into long-running goroutines so they can stop. A goroutine blocked forever on a channel that nobody will ever use is a goroutine leak.
go test -race or go run -race to detect data races; when state is shared, protect it with sync.Mutex or sync/atomic.A goroutine is scheduled by the Go runtime in user space, starts with a tiny growable stack and is much cheaper to create and switch than an OS thread. Many goroutines share a small number of threads.
Use channels to pass ownership of data or to coordinate stages of work. Use a sync.Mutex to protect a simple shared structure such as a cache or counter.
A send blocks until a receiver takes the value, and a receive blocks until a sender provides one. If another goroutine is already waiting on the other side, the operation completes immediately.
Goroutines cannot be killed from outside. Signal them with a closed channel or a cancelled context.Context, and have the goroutine return when it sees the signal.
Use go to run work concurrently, channels to hand values between goroutines, sync.WaitGroup to wait for completion, select for timeouts and cancellation, and context to stop work cleanly. Prefer unbuffered or small buffered channels, close channels only from the sender, and verify your code with the race detector.