Skip to main content
Module 7 · Lesson 1 of 531/50 in the course~12 min
Module lessons (1/5)

Goroutine: light parallelism

Lesson preview5 hands-on exercises readyCourse Go · Module 7 — Competition

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 go keyword and understand the lightweight model of Go's M:N scheduler.
  • Understand why main terminates 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

  1. 1Running a goroutine with go
  2. 2The process ends when main ends
  3. 3Capturing variables in loops
  4. 4Goroutine vs. Operating System Threads
  5. 5Avoid early exit goroutine leaks
  6. 6Guided practice
Pro Lesson · Module 7 — Competition

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.