Section 7 of 11

Goroutines & channels

go f() costs ~2KB. A million of them is normal.

A goroutine is not an OS thread. The runtime multiplexes them M:N onto a handful of real threads, and their stacks start tiny and grow. This is exactly what Java 21 virtual threads copied — Go has had it since day one, which is why the whole stdlib is written blocking-style.

reading the syntax
func() { process(job) }        // a function literal — a VALUE, not called
func() { process(job) }()      // the trailing () CALLS it
go func() { process(job) }()   // `go` runs that call in a new goroutine

Goroutines + WaitGroup

go.dev playground

Try it: Delete wg.Wait() and run again — main returns and kills every goroutine before they print.

Channels, select, and context cancellation

go.dev playground

Try it: Raise the worker sleep above the 200ms timeout and watch ctx.Done() win the select.