Goroutines vs threads: 8 differences that matter
Why Go can run a hundred thousand goroutines when the same number of OS threads would sink your machine, and how that compares with coroutines and Java virtual threads.
By devskills editorial team · Last updated
A goroutine is a function run concurrently by the Go runtime. An OS thread is run by the operating system kernel. Go runs many goroutines on a few threads. Here is the whole comparison in one table. Each row is explained below.
| Goroutine | OS thread | |
|---|---|---|
| Managed by | Go runtime (user space) | Operating system kernel |
| Starting stack | Small (2 KB), grows and shrinks as needed | Fixed, usually 1–8 MB reserved |
| Cost to create | Very cheap, no system call | Expensive, needs a system call |
| Switching cost | Cheap, done by the Go scheduler | Costlier, done by the kernel |
| Scheduling | Go scheduler, M:N (many goroutines on N threads), preemptive since Go 1.14 | Kernel scheduler, preemptive |
| How many you can run | Hundreds of thousands is normal | Usually thousands before memory hurts |
| Identity | No ID you can get at; on purpose | Has a thread ID |
| Usual way to talk | Channels (or a mutex when simpler) | Shared memory plus locks |
What is the difference between a goroutine and a thread?
A thread is scheduled by the operating system and costs megabytes of reserved stack. A goroutine is scheduled by the Go runtime, starts with a 2 KB stack, and many goroutines share a few threads. So goroutines are far cheaper to create and switch.
You still get real parallelism: the runtime uses as many threads for Go code as GOMAXPROCS says (by default, the number of CPU cores). The basics are in the Goroutines lesson (Goroutines).
Are goroutines cooperative or preemptive?
Preemptive. Since Go 1.14 the runtime has asynchronous preemption: it can stop a goroutine that runs too long (about 10 ms), even a tight loop with no function calls, by sending its thread a signal. Before 1.14 it was mostly cooperative.
Some older articles (including a well-known GeeksforGeeks page) still say goroutines use "cooperative scheduling". That was true of old Go versions, not of any Go you will install today. If an interviewer says it, you can politely mention Go 1.14.
package mainimport ( "fmt" "runtime" "time")func main() { runtime.GOMAXPROCS(1) // only one thread runs Go code go func() { for { // a busy loop that never calls a function or blocks } }() time.Sleep(20 * time.Millisecond) // Before Go 1.14 this line could hang forever: the loop never gave up the thread. fmt.Println("main still runs: the busy goroutine was preempted")}Why are goroutines so cheap?
Because they start with a tiny stack that grows only when needed, and creating or switching one never asks the kernel. A thread reserves a big stack up front and every switch goes through the kernel. That is why 100,000 goroutines is fine.
package mainimport ( "fmt" "sync" "sync/atomic")func main() { var wg sync.WaitGroup var total atomic.Int64 for i := 1; i <= 100_000; i++ { wg.Add(1) go func() { defer wg.Done() total.Add(int64(i)) }() } wg.Wait() fmt.Println("goroutines started: 100000") fmt.Println("sum of 1..100000 =", total.Load())}How does the Go scheduler work (GMP)?
The scheduler matches three things: G (goroutines), M (OS threads) and P (processors, one per GOMAXPROCS). Each P has a queue of runnable Gs and runs them on an M. Idle Ps steal work from busy ones.
When a goroutine blocks on a channel, a mutex or the network, the runtime parks it and runs another G on that thread. When it blocks in a system call, the P is handed to another thread so other goroutines keep running.
Why does Go not give goroutines an ID?
On purpose, so people do not build "thread-local storage" on top of goroutine IDs. In Go you pass what a goroutine needs as arguments, or in a context.Context for request-scoped values (Context).
When are threads still the answer?
In Go, almost never directly. You write goroutines and let the runtime choose threads. runtime.LockOSThread exists for the rare case where a C library or OS API needs the same thread every time, such as some GUI or graphics libraries.
Goroutine vs OS thread vs coroutine vs virtual thread
Goroutines get compared with three other ideas, and interviewers like to ask about all of them. The short version: goroutines and Java virtual threads are the same design (cheap threads scheduled by a runtime onto a few OS threads), while coroutines and async/await are cooperative and mark every place a task can pause.
| Goroutine (Go) | OS thread | Java virtual thread (JDK 21+) | Coroutine / async (Kotlin, Python, JS) | |
|---|---|---|---|---|
| Scheduled by | Go runtime | OS kernel | JVM, onto carrier threads | Language runtime or event loop |
| Pausing | Preemptive; blocking calls park it | Preemptive | Parks on blocking calls; no time-slice preemption | Cooperative, only at await / suspend points |
| Runs in parallel | Yes, on GOMAXPROCS threads | Yes | Yes, on the carrier pool | Python asyncio and JS: one thread. Kotlin: depends on the dispatcher |
| Stack | Own small stack that grows | Large fixed stack | Own stack, kept on the heap while parked | Usually stackless: state saved per suspend point |
| Marked in code | No: go f(), then ordinary blocking code | No | No: ordinary blocking Java code | Yes: async/await or suspend on every function in the chain |
| Talks through | Channels, or sync | Locks, shared memory | Locks, java.util.concurrent | Futures, channels (Kotlin), queues |
Goroutine vs OS thread: the one-line answer
An OS thread is the kernel's unit of scheduling, with a large fixed stack and a system call to create or switch. A goroutine is the Go runtime's unit, with a small growable stack, created and switched in user space, and multiplexed onto OS threads. Every goroutine runs on some OS thread while it is running; it just does not own one.
Goroutine vs coroutine: what is the difference?
A coroutine is a function that can suspend itself and be resumed later. In Python (async def) and Kotlin (suspend fun), suspension happens only at explicit points, so scheduling is cooperative: a coroutine that runs a long loop without awaiting blocks everything else on its thread. Most of these coroutines are stackless: the compiler turns the function into a state machine and saves only what is live at each suspension point.
A goroutine is not a coroutine in that sense. It has its own stack, it can be preempted anywhere (since Go 1.14), and goroutines run in parallel on several threads. You never mark where a goroutine may pause; a channel receive or a network read simply parks it. The name is a play on "coroutine", which is where the confusion comes from.
Goroutine vs Java virtual thread
Java 21 made virtual threads final (JEP 444). They are the closest thing to goroutines in another mainstream language: cheap threads managed by the JVM and run on a small pool of carrier OS threads, where a blocking call parks the virtual thread and frees the carrier. You write ordinary blocking code and still handle a very large number of concurrent tasks.
The differences are in the details. Virtual threads were added to a language whose libraries assumed expensive threads, so some code paths pin a virtual thread to its carrier: before JDK 24 (JEP 491), blocking inside a synchronized block did this, and native calls still do. Virtual threads are also not time-sliced, so a CPU-bound loop keeps its carrier until it blocks, whereas the Go runtime preempts long-running goroutines. Go has had this model since version 1.0, so the whole standard library was written for it.
ExecutorService with a fixed pool maps onto a worker pool, and a BlockingQueue maps onto a buffered channel.Goroutines vs async/await
With async/await (JavaScript, C#, Python, Rust), a function that waits has to be marked async, and so does every function that awaits it. That split is often called function colouring: you end up with two kinds of function that do not mix freely. The upside is that every point where your code can pause is visible.
Go has no colours. A function that reads from the network looks exactly like one that adds two numbers. When it blocks, the runtime parks the goroutine and uses its network poller to resume it when data arrives, which is what an event loop does, hidden behind blocking calls. The cost is that pause points are not visible in the code, so shared state needs a mutex or a channel even where an async language would have kept it on one thread.
- Explain the 8 differences between a goroutine and an OS thread in an interview.
- Describe how the Go scheduler runs many goroutines on a few threads, and how it preempts them.
- Compare goroutines with coroutines, Java virtual threads and async/await in one sentence each.
- Run a program that starts 100,000 goroutines and prints the result.
- Goroutines are run by the Go runtime; threads by the OS.
- Java virtual threads are the same idea; coroutines and async/await are cooperative and marked in the code.
- Goroutines start at 2 KB of stack and grow; threads reserve megabytes.
- The scheduler runs many goroutines on GOMAXPROCS threads (M:N, the GMP model).
- Goroutines are preemptive since Go 1.14, even in tight loops. "Cooperative" is outdated.
- There is no goroutine ID; pass data in arguments or a context.
- 1Run
preempt.gowithruntime.GOMAXPROCS(1). Why does main still print? - 2Change
many.goto printruntime.NumGoroutine()just beforewg.Wait(). Why does the number change from run to run? - 3In one sentence each, name three things that make a goroutine cheaper than a thread.