Module lessons (1/5)
Goroutine: light parallelism
go f(), cost of a goroutine, why the main ends without waiting and how to synchronize.
What you'll learn in this lesson
- Start concurrent streams with the
gokeyword and understand the lightweight model of Go's M:N scheduler. - Understand why
mainterminates the entire process without waiting for launched goroutines if synchronization is missing. - Safely capture loop variables in goroutines according to Go 1.22+ rules and the explicit pattern for older versions.
Overview
A goroutine is an execution unit managed by the Go runtime, much cheaper than an operating system thread: it starts with just ~2 KB of stack that grows dynamically and the runtime multiplexes tens of thousands of them onto a few OS threads.
Prepending go to any function call (named or anonymous) schedules it asynchronously and immediately returns control to the caller:
Sections covered in this lesson
- 1Running a goroutine with
go - 2The process ends when
mainends - 3Capturing variables in loops
- 4Goroutine vs. Operating System Threads
- 5Avoid early exit goroutine leaks
- 6Guided practice
Unlock "Goroutine: light parallelism" and the full Course Go curriculum
`go f()`, cost of a goroutine, why the main ends without waiting and how to synchronize.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Go plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.