Skip to content
IT glossary
BackendUpdated December 3, 2026

Goroutine

Горутина

A lightweight unit of execution in Go that lets you start a function without waiting for it to finish. The Go runtime schedules goroutines on operating system threads.

What is a goroutine?

A regular call to work() holds up the caller until the function returns. Put go in front of the call, and Go runs the function in a new goroutine while the caller moves on. In a backend service, that can let you start a second request to an upstream API while the first is still waiting for a response.

This is concurrency: more than one task is in progress over the same period. It isn't necessarily parallelism, where tasks execute at the same instant. Two goroutines can take turns on one CPU core, and starting one first doesn't guarantee it will run first.

What the runtime does

The Go scheduler maps runnable goroutines onto operating system threads. A goroutine doesn't need a dedicated thread. If it blocks receiving from a channel, the runtime can use that thread to run other goroutines. Each goroutine starts with a small stack that can grow when needed. That makes goroutines inexpensive compared with dedicating a thread to each task, but not free.

Starting a goroutine and waiting for it are separate operations. A go statement doesn't give the caller a return value or a handle to wait on. Use a channel to receive a result or a sync.WaitGroup to wait for a group of tasks to finish. Returning from main ends the program even if other goroutines are still running.

Example: hand a result back to the caller

package main

import "fmt"

func main() {
    result := make(chan int)
    go func() {
        result <- 6 * 7
    }()
    value := <-result
    fmt.Println(value)
}

The channel is unbuffered, so the send can't complete until a receiver is ready, and the receive can't complete until there's a sender. The scheduler can run either side first; the receive ensures that main gets 42 before printing and returning. The synchronization comes from the channel, not from the go statement.

Where goroutines help and what can go wrong

Typical uses include making independent network calls, processing queued jobs, and doing background work. Think about what limits the number of active tasks. If you start a goroutine for every incoming job, a burst can exhaust database connections even if the goroutines themselves use little memory. A fixed-size worker pool can cap the number of jobs in progress.

Also decide how each goroutine will exit. A sender can get stuck forever if its consumer stops reading. One fix is to use select to wait for either a successful send or cancellation through ctx.Done(). Cancellation is cooperative: the goroutine or the operation it calls has to observe it. Finally, goroutines share memory, so concurrent access to a variable that one goroutine writes needs synchronization, such as a mutex.

In interviews

For a snippet with two goroutines, don't guess the output based on source order. Trace which operations can block, what can unblock them, and whether main waits long enough for the output to appear.

“Goroutine” questions from real interviews

Asked at 138 companies

All Golang questions