A goroutine is a function running concurrently under the control of the Go runtime, while a thread is a unit of execution scheduled by the operating system kernel. Goroutines start with a stack of a few kilobytes, are multiplexed onto a small pool of OS threads, and are cheap enough that programs routinely run hundreds of thousands of them. Use goroutines for almost all concurrency in Go, and think about OS threads only when you call into C code or need thread-bound state.
The two are not competitors. Every goroutine eventually runs on an OS thread, but the Go scheduler decides which one and when, so you do not create or manage threads yourself.
| Aspect | Goroutine | OS thread |
|---|---|---|
| Managed by | Go runtime scheduler | Operating system kernel |
| Initial stack | Small (a few KB), grows and shrinks on demand | Fixed, typically 1-8 MB reserved |
| Creation cost | Very low, a function call plus a small allocation | Higher, requires a kernel call |
| Context switch | Cheap, done in user space | More expensive, enters the kernel |
| Practical count | Hundreds of thousands or millions | Thousands before problems appear |
| Identity | No public ID, no thread-local storage | Has a thread ID and thread-local storage |
| Communication | Channels, sync primitives | Shared memory plus locks, condition variables |
The exact stack sizes and limits depend on the platform and Go version, but the order of magnitude difference is stable: goroutines are far smaller and cheaper.
Go uses an M:N scheduler, often described with three letters:
An M must hold a P to run Go code. The number of Ps defaults to the number of logical CPUs and is controlled by GOMAXPROCS. When a goroutine blocks on a channel, a mutex or a timer, the scheduler parks it and runs another goroutine on the same thread without involving the kernel. When a goroutine makes a blocking system call, the runtime hands the P to another thread so the remaining goroutines keep running. Since Go 1.14 the scheduler can also preempt long-running goroutines asynchronously, so a tight loop cannot starve the others.
You can inspect these values at run time:
Passing 0 to runtime.GOMAXPROCS only reads the current value. Since Go 1.25, the default GOMAXPROCS also respects Linux container CPU limits, which matters when you run in Kubernetes.
The go keyword starts a goroutine, and sync.WaitGroup waits for a group of them. This program starts 100,000 goroutines, something that would be unreasonable with one OS thread each:
for range 100_000 (range over an integer) needs Go 1.22 or later. Since Go 1.25 you can also write wg.Go(func() { ... }) instead of the Add, go and Done triple. The atomic counter avoids a data race; run your tests with go test -race to catch the ones you miss.
Threads usually communicate by sharing memory protected by locks. Go encourages the opposite: pass data through channels so that only one goroutine owns a value at a time.
Mutexes from sync remain the right tool for protecting a small piece of shared state, such as a cache. Choose channels for handing off work and ownership, and mutexes for guarding data structures.
Use goroutines for:
GOMAXPROCS workers.OS threads become relevant when:
runtime.LockOSThread to pin a goroutine to its thread.A common mistake is leaking goroutines: a goroutine blocked forever on a channel is never garbage collected. Pass a context.Context and select on ctx.Done() so every goroutine has a way to exit.
No. A goroutine is a task scheduled by the Go runtime. It runs on top of OS threads, but there is no fixed one-to-one relationship, and one thread can run many goroutines over its lifetime.
Limited mainly by memory, since each needs at least a small stack. Hundreds of thousands are common and millions are possible on a machine with enough RAM, but you should still bound concurrency for work that touches limited resources such as database connections.
They are similar in spirit. Both are lightweight tasks scheduled in user space on a few carrier threads. Goroutines are built into Go from the start, while Java virtual threads were added in Java 21.
Yes. With GOMAXPROCS greater than 1, goroutines run on several CPU cores at the same time. Concurrency is the structure of the program; parallelism is the actual simultaneous execution.
Goroutines are lightweight, runtime-scheduled functions multiplexed onto a few OS threads by the Go scheduler. They start small, switch cheaply and communicate through channels, so you can run far more of them than threads. Default to goroutines, bound their number for scarce resources, give each one an exit path with context, and reach for thread-level controls only for cgo and thread-bound libraries.