Go's standard library covers parallel computing with goroutines, channels, the sync and sync/atomic packages, and context, and it covers distributed computing with net, net/http, and encoding/json. Parallelism means running work on several CPU cores at once, which the Go scheduler does for you through GOMAXPROCS. Distribution means splitting work across processes or machines that talk over a network, which Go handles with sockets and HTTP.
The standard library gives you building blocks rather than a cluster framework. For production distributed systems you combine these blocks with patterns such as worker pools, fan-out and fan-in, timeouts, and retries.
synccontextnet and net/http
A goroutine is a function running concurrently, started with the go keyword. The runtime multiplexes thousands of goroutines onto a small number of OS threads, and it uses all available cores by default (GOMAXPROCS equals the number of logical CPUs).
Concurrency and parallelism are different. Concurrency is about structuring a program as independent tasks, and parallelism is about executing them at the same moment. Go gives you the first, and the scheduler provides the second when cores are free.
Never use time.Sleep to wait for a goroutine. Wait with a sync.WaitGroup or a channel instead, as in the next sections.
syncChannels pass data between goroutines and synchronise them at the same time. A send on an unbuffered channel blocks until another goroutine receives.
Use sync.WaitGroup to wait for a known set of goroutines. Since Go 1.22 each loop iteration has its own variable, so capturing i in a closure is safe. Go 1.25 added WaitGroup.Go, which replaces the Add and Done bookkeeping.
Writing to distinct slice elements is safe. When goroutines share one value, protect it with a sync.Mutex or use sync/atomic.
Run your tests with go test -race to catch unsynchronised access. The race detector is the most useful tool for parallel Go code.
A worker pool limits how many goroutines work at once. This matters for CPU-bound jobs, where more goroutines than cores add overhead, and for I/O-bound jobs that hit a rate-limited service.
The pattern has three parts. The producer sends jobs and closes the channel. Workers fan out by reading from the same channel. The collector fans in by reading all results from one channel. Because both channels are buffered to hold every item, the program cannot deadlock here.
contextEvery parallel or networked operation needs a way to stop. The context package carries deadlines and cancellation signals through your call tree.
When several goroutines can fail, the golang.org/x/sync/errgroup package (maintained by the Go team, outside the standard library) cancels the siblings on the first error and returns it.
net and net/httpThe net package gives you TCP, UDP, and Unix sockets. Most services today use net/http instead, because HTTP gives you routing, TLS, and HTTP/2 without extra code. Since Go 1.22 the default ServeMux supports methods and path parameters.
A client calls this with an http.Client that always sets a timeout, because the default client has none:
net/rpc?The standard library still ships net/rpc, but it is frozen: it uses Go-only encoding/gob and receives no new features. For cross-language services use gRPC or JSON over HTTP, and use net/rpc only for small Go-to-Go tools.
| Strategy | Use it when | Standard library tools |
|---|---|---|
| Divide and conquer | A large dataset splits into independent chunks | goroutines, sync.WaitGroup |
| Worker pool | You must cap concurrency | buffered channels, sync |
| Fan-out and fan-in | A pipeline has stages of different speed | channels |
| Timeouts and cancellation | Calls cross a network | context, http.Client.Timeout |
| Retries with backoff | Remote calls fail transiently | time, context |
| Graceful shutdown | Services must finish in-flight work | http.Server.Shutdown, os/signal |
Distribute work by partitioning data (for example by hashing a key to pick a node) and make operations idempotent so retries are safe. A load balancer in front of identical instances is simpler and usually more robust than client-side balancing code.
Yes. The runtime spreads goroutines across OS threads, and GOMAXPROCS defaults to the number of logical CPUs. You only need to set it yourself in special cases, such as containers with CPU limits on older Go versions.
For CPU-bound work, about one per core. For I/O-bound work, as many as the remote system tolerates. Goroutines are cheap, but unbounded creation still exhausts memory and downstream services.
Use a mutex to protect shared state such as a counter or a cache. Use channels to transfer ownership of data or to signal events between goroutines.
For services that talk over HTTP, mostly yes. For consensus, service discovery, or message queues you need external systems such as etcd, NATS, or Kafka and their Go clients.
Go's standard library gives you goroutines and the scheduler for parallelism, channels, sync, and sync/atomic for coordination, context for cancellation, and net/http for distribution. Use worker pools to cap concurrency, always set timeouts on network calls, test with go test -race, and prefer HTTP or gRPC over the frozen net/rpc package for new services.