Before this:Interfaces & composition
Goroutines
Key takeaways
A goroutine is a function launched with the go keyword that runs
concurrently with the rest of your program. Goroutines are lightweight — managed
by Go’s runtime, starting at a few kilobytes — so a program can run thousands. This
is Go’s headline feature, and it’s why a real-time engine like GopherTrunk can juggle
many signal streams at once.
Everything so far has run top to bottom, one line at a time. This lesson introduces concurrency: doing several things at once, the Go way.
Launching a goroutine
Put go in front of a function call and it runs concurrently — the calling code
does not wait for it:
go processSamples(stream) // runs alongside everything below
fmt.Println("kicked off processing")
processSamples now runs independently while main carries on. That one keyword is
the entire syntax for starting concurrent work.
Why goroutines are cheap
A goroutine is not an operating-system thread. Go’s runtime keeps a small pool of real threads and schedules many goroutines onto them, each starting with a tiny stack that grows only if needed.
Because they are so cheap, the idiomatic Go move is to launch a goroutine per unit of concurrent work rather than carefully pooling a scarce resource. GopherTrunk uses this to run capture, decoding, and per-channel processing side by side.
The catch: goroutines need coordination
A goroutine runs on its own, which raises two questions this unit answers next:
- How do goroutines share results safely? Two goroutines touching the same variable at once is a data race — a real bug. The answer is channels, Go’s built-in way to pass data between goroutines.
- How does
mainwait for them? Ifmainreturns, the program exits and any running goroutines are cut off. You need a way to coordinate — covered in select & synchronization.
Go’s motto captures the philosophy: don’t communicate by sharing memory; share memory by communicating. That’s the channel model, and it’s next.
Quick check: why can a Go program run thousands of goroutines at once?
Recap
- A goroutine is launched with the
gokeyword and runs concurrently. - Goroutines are lightweight, scheduled by Go’s runtime onto a few OS threads.
- You can run thousands cheaply — the idiomatic way to structure concurrent work.
- They need coordination: channels to share data, and synchronization to wait.
Next up: channels, the safe way for goroutines to talk to each other.
Frequently asked questions
What is the difference between a goroutine and a thread?
A goroutine is a function managed by Go’s own runtime scheduler, not directly by the operating system. Goroutines start with a tiny stack (a few kilobytes) that grows as needed, so you can run tens of thousands of them; OS threads are heavier, so you typically have far fewer. Go multiplexes many goroutines onto a small pool of real threads for you.
Is concurrency the same as parallelism?
No. Concurrency is structuring a program as independently running tasks; parallelism is those tasks literally executing at the same instant on multiple CPU cores. Goroutines give you concurrency; whether they run in parallel depends on how many cores are available. Go handles the mapping automatically.