Skip to content
Learn Netverks

Lesson

Step 22/36 61% through track

context-package

Context package

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

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

Teams still ship Context package in Go codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Context package in contexts like: Kubernetes ecosystem tools, cloud APIs, and CLI utilities.

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).

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

context.Context carries deadlines, cancellation signals, and request-scoped values across API boundaries—essential in HTTP handlers and gRPC services.

Cancellation

ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50)
defer cancel()
select {
case <-ctx.Done():
    fmt.Println(ctx.Err())
}

Pass ctx as the first parameter to functions that may block—never store contexts in structs long-term.

Production usage

Kubernetes operators, cloud SDKs, and microservices propagate cancellation when clients disconnect—prevents goroutine leaks.

Important interview questions and answers

  1. Q: Why context first parameter?
    A: Convention for cancellation propagation—tools and linters expect it.
  2. Q: context.WithValue abuse?
    A: Use sparingly for request metadata—not a general dependency injection bag.

Self-check

  1. What does WithTimeout return?
  2. Why defer cancel()?

Context tree

Derive child contexts with WithCancel, WithTimeout, or WithDeadline. Cancelling a parent cancels children—propagate from HTTP request context downward.

Avoid context.WithValue for optional parameters—use explicit function args. Values are for request-scoped IDs traceable across middleware.

Tip: Pass context.Context as the first parameter—never store it in structs; cancel propagates down the call tree.

Interview prep

Context first param?

Convention for cancellation/deadline propagation—linters and stdlib expect it.

WithValue abuse?

Use sparingly for request-scoped metadata—not a general DI container.

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

  • context.Cancel?
  • Timeout pattern?

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