Goroutines are lightweight functions that the Go runtime runs concurrently, and channels are typed pipes that let goroutines send values to each other safely. Start a goroutine with the go keyword, and use channels (or sync primitives) to share data and coordinate work. Concurrency is about structuring independent tasks; parallelism happens when the runtime schedules them on several CPU cores at once.
The Go proverb sums up the design: do not communicate by sharing memory; share memory by communicating.
Put go before a function call to run it concurrently. The caller does not wait, so you must synchronise or the program may exit first.
A goroutine starts with a small stack that grows as needed, so programs can run thousands or even millions of them. Since Go 1.22 each loop iteration has its own i, which is why the closure above is safe. Go 1.25 added wg.Go(func() { ... }) as a shorthand for the Add, go and Done sequence.
A channel is created with make(chan T). Sends and receives block until the other side is ready (unbuffered) or until the buffer is full or empty (buffered).
Only the sender should close a channel. Sending on a closed channel panics, while receiving from one returns the zero value immediately; use v, ok := <-ch to detect closure.
Function signatures can restrict direction: chan<- int is send-only and <-chan int is receive-only. This documents intent and lets the compiler catch mistakes.
Three workers read from jobs, the main goroutine closes jobs when it has no more work, and results is closed only after every worker has finished.
select waits on several channel operations and runs the first that is ready. Combine it with context for timeouts and cancellation.
Every goroutine that can block should also watch ctx.Done(), otherwise it may leak when the caller gives up.
By default the runtime uses GOMAXPROCS threads, which equals the number of CPU cores, so goroutines run in parallel when work is available. When goroutines share variables you need protection.
| Tool | Use it for |
|---|---|
| Channel | Passing ownership of data, pipelines, signalling |
sync.Mutex and RWMutex | Protecting shared state |
sync/atomic | Simple counters and flags |
sync.WaitGroup | Waiting for a group of goroutines |
errgroup (golang.org/x/sync) | Goroutines that return errors and cancel together |
Run tests with go test -race to detect data races. Common mistakes are forgetting to close channels that are ranged over, deadlocks from unmatched sends, and goroutines that never exit.
A goroutine is managed by the Go runtime, starts with a small growable stack, and is multiplexed onto a few OS threads. Threads are heavier and scheduled by the operating system.
Use a mutex to protect a data structure that several goroutines read and write. Use channels to transfer data or signal events between goroutines.
A goroutine is waiting on a channel that nobody will ever send to or receive from. The runtime reports "all goroutines are asleep" when every goroutine is blocked.
Both. They are concurrent by design and run in parallel when several CPU cores are available.
Use go to start goroutines, channels to pass values between them, select with context for timeouts and cancellation, and sync tools to protect shared state. Always know how each goroutine ends, close channels from the sender side, and test with the race detector.