goroutine
A function running concurrently with the rest of a Go program, started by putting the go keyword before a call.
In more detail
Goroutines are very cheap, so a tool can start one per target. The caller carries on immediately, and when main returns the program exits whether or not goroutines have finished, so wait for them with a sync.WaitGroup or a channel. The Go runtime schedules them across the machine's processor cores.
Example
go func() {
defer wait.Done()
fmt.Println("worker finished")
}()Related terms
- concurrency — Having several pieces of work in progress during overlapping periods of time, such as requests being handled at once.
- sync.WaitGroup — A counter from Go's sync package that lets one goroutine wait until a group of other goroutines have all finished.
- channel — A typed pipe in Go that one goroutine sends values into and another receives them from, safely handing data between them.
- Go scheduler — The part of the Go runtime that decides which goroutine runs on which operating system thread, and when.
- goroutine leak — A goroutine that never finishes because it is stuck waiting for something that will never happen, slowly wasting memory.
Taught in
- Panics and Recover — Concurrent Systems Programming with Go
- Goroutines and Concurrent Execution — Concurrent Systems Programming with Go
- Channels and Synchronization — Concurrent Systems Programming with Go
- The select Statement — Concurrent Systems Programming with Go
- Context Cancellation and Timeouts — Concurrent Systems Programming with Go
- Project Setup and Configuration — Concurrent Systems Programming with Go