liveBook · Manning

Goroutine

As explained in Go by Example

A goroutine is a lightweight Go execution unit that runs a function concurrently with other goroutines.

A goroutine is an independently running concurrent function. Prefix a function call with go to schedule it concurrently; ordinary function syntax otherwise stays the same. Goroutines are lightweight, so Go can run very large numbers without requiring a traditional thread pool. section 1.4.1 section 1.10

Start concurrent work

A web crawler can launch separate crawls without changing the crawl function itself: go crawl("...") go crawl("...") Each call runs in its own goroutine while the work inside crawl remains sequential. section 1.4.1

Figure 1.3 The go statement before a function spawns a new goroutine that runs the given function concurrently. This figure illustrates running the crawl function concurrently in two separate goroutines.
A diagram of a graph

AI-generated content may be incorrect.

The diagram shows how placing go before a function call spawns separate concurrent executions of that function. section 1.4.1

Goroutines abstract operating-system threads. The Go scheduler multiplexes them onto threads, parks a goroutine that blocks on a channel or I/O operation, and runs another ready goroutine. Their small, adjustable stacks and user-space scheduling reduce the cost of concurrent work, though the scheduler’s full internals are beyond this overview. section 1.4.2

Use goroutines with an explicit communication and lifecycle plan:

  • Send data, tasks, and results through channels when goroutines need to communicate or synchronize; channel operations can pause goroutines to coordinate stages. section 1.4.3
  • Remember that goroutines share memory: channels do not provide memory isolation, so shared memory still requires correct use. section 1.4.3
  • Use worker goroutines for concurrent HTTP load, and use a monitoring goroutine to close an output channel only after workers finish. section 6.9

A practical use appears in HTTP servers: net/http runs a registered Handler’s ServeHTTP method in a separate goroutine for each request. This works efficiently because the scheduler multiplexes goroutines onto operating-system threads. section 8.3

How Go by Example covers it

How deep it goes
Defines goroutines, demonstrates go calls, and outlines scheduler behavior while deferring full scheduler internals.
Examples
Demonstrates concurrent crawl calls, dispatcher workers and a monitor, and HTTP handlers running per request.
What it assumes
Introduces concurrent execution from a sequential function call, without stated prior concurrency requirements.
What you can do after
Apply goroutines to launch concurrent work and coordinate completion through channels.
How it connects
Connects goroutines to channels, the Go scheduler, operating-system threads, nonblocking I/O, and HTTP handlers.

Also explained in

Compare how 7 books explain goroutine →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go by Example. The section links point to the parts of the book it draws on.