Skip to content
Learn Netverks

Lesson

Step 18/36 50% through track

goroutines

Goroutines

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches Goroutines: the syntax, patterns, and safety habits you need before advancing in Go.

Concurrency is Go’s headline feature—know when to use channels vs mutexes and how to avoid leaks.

You will apply Goroutines in contexts like: Worker pools, fan-out/fan-in pipelines, and cloud controllers.

Write Go in main.go with package main and func main(), click Run on server—the dev runner runs go run main.go; use fmt.Println for output (requires Go toolchain; LEARNING_RUNNER_ENABLED=true). Also keep playground examples short so they finish before the runner timeout.

When you can explain the previous lesson's ideas without copying starter code.

A goroutine is a lightweight concurrent function started with the go keyword. The Go runtime multiplexes goroutines onto OS threads—far cheaper than one thread per task in Java.

Starting goroutines

go func() {
    fmt.Println("async")
}()
time.Sleep(time.Millisecond) // wait in playground demos

Main exits immediately without waiting—use channels, sync.WaitGroup, or time.Sleep in lessons so output appears before exit.

Compared to other models

  • Node.js — single-threaded event loop; Go runs true parallel goroutines on multiple cores
  • Python — GIL limits CPU parallelism in threads; Go goroutines scale for CPU and I/O
  • Rust — async/threads with ownership; Go GC simplifies shared heap (with sync discipline)

Important interview questions and answers

  1. Q: Goroutine vs OS thread?
    A: Goroutines are smaller, scheduled by Go runtime on a thread pool—millions are practical.
  2. Q: What happens if main returns while goroutines run?
    A: Program exits—unfinished goroutines are terminated.

Self-check

  1. What keyword starts a goroutine?
  2. Why use WaitGroup or channels instead of Sleep in production?

Tip: Main exits without waiting for goroutines—use channels, WaitGroup, or brief time.Sleep in playground demos so output appears.

Interview prep

Goroutine vs thread?

Goroutines are lightweight, scheduled by Go runtime on OS thread pool—millions are practical.

Main exits early?

Program terminates—unfinished goroutines are killed; use sync primitives to wait.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Goroutine vs thread?
  • Leak example?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump