Goroutine
PronunciationGOH-roo-teen
Definition
A goroutine is Go's lightweight unit of concurrent work. You start one with the go keyword, and the Go runtime schedules thousands of them over a few operating system threads.
Where you hear it
In Go code and tutorials, interviews for Go roles, and concurrency discussions comparing Go with async or threads.
Examples
Launch a goroutine per request and send results over a channel.
Leaking goroutines that wait forever is a common bug.
Each incoming job runs in its own goroutine and reports back on a channel.
Common mistake
Starting goroutines with no way to stop or wait for them. Use a WaitGroup, context or channel to control them.
Don't confuse with
An operating system thread, which is far heavier. Goroutines start small and are cheap to create in large numbers.
Say it at work
- In a meeting or chat
Run this in a goroutine.
- In an email or ticket
Use
go test -raceto check the goroutines.
Keep exploring
More in Programming Fundamentals