Programming languages / Goroutines and channels

Goroutines and channels

How Go runs many functions side by side and lets them hand values to each other. Starting a goroutine, sending on a channel, buffers, select, and why Go prefers messages to shared variables.

Intermediate7 steps~7 min
main — the goroutine every program starts with
Goroutine — a function running alongside the others
Channel — a typed pipe between goroutines
Value — what is sent through a channel
// THE SHORT VERSION

In 30 seconds

A goroutine is a function started with the word go. It runs alongside the rest of the program and is cheap enough that programs start thousands.

Goroutines pass values through channels, which are typed pipes. An unbuffered channel makes sender and receiver meet; a buffered one holds a few values so the sender can run ahead.

select waits on several channels and takes whichever is ready first. The habit Go encourages is to give data to one goroutine and send it messages, rather than have many goroutines change the same variable.

Goroutines and channels, step by step

STEPS01 / 07

Stack sizes are typical figures and vary by system and Go version. Go also has locks (sync.Mutex), and for simple shared state they are sometimes the plainer tool.

STEP 01 / 07 go starts a goroutine
THE MATH
go work() starts work, does not wait
  1. Step 1: go starts a goroutine. In Go, writing go in front of a function call starts that function as a goroutine: an independent line of execution managed by the Go runtime. The caller does not wait for it. main moves straight on to its next line while work runs alongside.
  2. Step 2: Goroutines are cheap. An operating-system thread typically reserves a megabyte or more for its stack. A goroutine starts with about 2 kilobytes and grows only if it needs to. The Go runtime runs many goroutines on a few threads, so a program can afford thousands of them.
  3. Step 3: A channel carries values. Goroutines talk through channels. A channel is a typed pipe: one goroutine sends a value in with ch <- 42, another takes it out with v := <-ch. This channel is declared chan int, so it carries whole numbers and nothing else.
  4. Step 4: Unbuffered: a handshake. A channel made without a size has no room to hold anything. A send stops and waits until another goroutine is ready to receive, and a receive waits for a sender. The value is handed over directly, so the two goroutines meet at that moment.
  5. Step 5: Buffered: a small queue. Give make a size and the channel gets a buffer: a queue with that many slots. Sends succeed at once while there is room, so the sender can run ahead. Here 1 and 2 are stored; the third send waits until a receiver takes one out. Values leave in the order they went in.
  6. Step 6: select waits on several. select lets a goroutine wait on several channel operations at once and run whichever becomes ready first. Here nothing has arrived on a, b has a 7 waiting, and the one-second timer has not fired, so the b case runs. If several are ready, Go picks one at random.
  7. Step 7: Share by communicating. When two goroutines change the same variable at once, results get lost unless every access is guarded by a lock. Go’s advice is to turn that around: let one goroutine own the data and have the others send it messages over a channel. Only the owner ever touches count.
VALUES SO FAR
go f()new goroutinekeyword
stack····cheap
ch <- v····channel
<-ch····channel
no size····unbuf.
size 2····buffered
select····rule
count····rule

Spotted a mistake on this page?