Skip to content

Goroutine

Category: Units of execution · Status: stub · Lessons: chapter 01, Threads

One line: Go's unit of concurrency: a function call started with the go statement and scheduled by the Go runtime onto a small pool of operating-system threads.

How it connects

flowchart LR
  n_goroutine["Goroutine"]
  n_green_thread["Green threads and M:N scheduling"]
  n_goroutine -->|is a| n_green_thread
  classDef center stroke-width:3px
  class n_goroutine center
  classDef outside stroke-dasharray: 4 3
  class n_green_thread outside

In each language

Rust tokio::spawn is the nearest, and returns a JoinHandle
Go The go statement ↗ discards the function's results, and a goroutine has no ID ↗ to hold on to
Java Virtual threads ↗ are the nearest: scheduled by the Java runtime, but still Thread objects that can be joined
Kotlin launch returns a Job, and Job.join suspends until the coroutine completes
Erlang and Elixir spawn returns a pid, which is used to send the process messages
Haskell forkIO returns a ThreadId to kill it by; waiting for it is written by hand, for instance with an MVar

Where to read more