These Go context interview questions cover context.Background, cancellation, timeouts, deadlines, context values and context in HTTP and database code. We gathered them from public interview lists, coding rounds and community threads. Then we rewrote them in plain words and grouped them by idea.
This post is questions only, so answer each one in your own words first. When you need help, read Go context for beginners, Go HTTP client and Go graceful shutdown. Also, finish Part 16 on the sync package first, because context often works next to WaitGroup and errgroup.
How to Use These Go Context Interview Questions
Answer each of these Go context interview questions out loud, then write a small program with a short timeout. Context bugs usually show up as slow requests or goroutines that never stop. Finally, try the scenario questions, since they come from real production systems.
The table below lists each section and its number of questions.
| Section | What it tests | Questions |
|---|---|---|
| Context basics | Background, the context tree and rules | 15 |
| Cancellation | WithCancel, cancel causes and the tree | 15 |
| Timeouts and deadlines | WithTimeout, WithDeadline and budgets | 15 |
| Context values | Keys, request IDs and good use | 14 |
| HTTP, DB and gRPC | Context across a real call chain | 15 |
| Context coding tasks | Classic interview programs | 13 |

Go Context Interview Questions on the Basics
Before you answer, study Go context for beginners. These Go context interview questions check the rules that the whole Go ecosystem follows.
- What is context.Context, and why did Go add it to the standard library?
- Which four methods does the Context interface have?
- What is the difference between context.Background and context.TODO?
- Why should ctx be the first parameter of a function?
- Why should you not store a context inside a struct?
- What does it mean that contexts form a tree?
- Can a child context live longer than its parent?
- Is a context safe to use from many goroutines at once?
- Why should you never pass a nil context, even if a function allows it?
- How does a function signal that it respects cancellation?
- Why do many library functions come in pairs, such as Query and QueryContext?
- What does ctx.Err return before and after cancellation?
- How does go vet help with the lostcancel check?
- Scenario: when a new helper takes no context but calls the database, how would you change its signature?
- Scenario: when a team stores the request context in a long-lived service struct, what bugs can appear?
Go Context Interview Questions on Cancellation
Before you answer, study Go goroutines for beginners. Cancellation is the main job of context, so these Go context interview questions go into detail.
- What does context.WithCancel return?
- Why must you always call the cancel function, even when work succeeds?
- What happens to child contexts when you cancel a parent?
- Does cancelling a child affect its parent or its siblings?
- How does a goroutine notice cancellation through ctx.Done?
- Why is ctx.Done a channel instead of a bool?
- Which feature did WithCancelCause add in Go 1.20, and how do you read the cause?
- What does context.AfterFunc do, and when is it useful?
- What does context.WithoutCancel do in Go 1.21, and when would you need it?
- Can a CPU-heavy loop see cancellation, and how often should it check?
- Why does cancellation not stop a goroutine by force?
- How do you cancel all workers when one of them fails?
- How does errgroup.WithContext tie errors and cancellation together?
- Scenario: when a user closes the browser tab, how would your handler stop the database query?
- Scenario: when an audit log write must finish even after the request ends, how would you detach it from cancellation?
Go Context Interview Questions on Timeouts and Deadlines
Before you answer, study Go HTTP client and Go retry with backoff. Timeouts protect every service call, so these Go context interview questions appear in most backend rounds.
- How do WithTimeout and WithDeadline differ?
- What does ctx.Deadline return when no deadline exists?
- What error does ctx.Err return after a timeout?
- How do you check for a timeout with errors.Is and context.DeadlineExceeded?
- Can a child context set a longer timeout than its parent, and what really happens?
- How do you split one request budget across three downstream calls?
- Why should retries respect the remaining deadline?
- How does http.Client.Timeout differ from a context timeout?
- What do WithTimeoutCause and WithDeadlineCause add?
- Why should a server set timeouts on its own http.Server fields too?
- How do you pass a deadline across services, for example in gRPC?
- What is the risk of a timeout that is too short under load?
- How do you test timeout paths quickly without slow tests?
- Scenario: when a payment call hangs and holds a worker forever, how would you add a safe timeout?
- Scenario: when a downstream service is slow, how would you stop timeouts from piling up into retries?
Go Context Interview Questions on Values
Before you answer, study Go middleware examples, because middleware is where most context values start. These Go context interview questions check whether you know what belongs in a context and what does not.
- What does context.WithValue do?
- Why should a context key be an unexported custom type?
- What can go wrong when two packages use the string key “user”?
- What kind of data belongs in context values, for example a request ID?
- Why should you not pass optional function parameters through context?
- How does ctx.Value search for a key through the tree?
- What does ctx.Value return when the key is missing?
- How do you write typed helper functions to set and get a value?
- Why is a long chain of WithValue calls slow to search?
- How do middleware and handlers share a user ID through context?
- How do you add a request ID to every log line with slog and context?
- Are context values safe to change after you add them?
- Scenario: when a reviewer finds a database handle inside ctx.Value, how would you refactor it?
- Scenario: when logs miss the trace ID in some functions, how would you find the place that dropped the context?
Context in HTTP, Database and gRPC Code
Before you answer, study Go database tutorial and Go HTTP server. These Go context interview questions follow a real request from the client to the database.
- Where does an HTTP handler get its context from?
- When does the server cancel r.Context for a request?
- How do you build an outgoing request with http.NewRequestWithContext?
- What does db.QueryContext do when the context ends during a query?
- Why should a transaction use the same context for every statement?
- In gRPC, how do deadlines and cancellation travel between services?
- How does signal.NotifyContext help with graceful shutdown?
- How do you stop background jobs when the server receives SIGTERM?
- What does t.Context give you in tests since Go 1.24?
- How does a message queue consumer use context for each message?
- Why should you not use the request context for work that outlives the request?
- How do you pass context into a worker pool safely?
- How do OpenTelemetry spans travel through context?
- Scenario: when a client disconnects but the server keeps sending a big report, how would you stop the work?
- Scenario: when a service shuts down and drops jobs halfway, how would you use context to finish cleanly?
Go Context Interview Questions for Coding Rounds
Before you answer, write each program by hand and test it with a short timeout. Interviewers watch where you call cancel and how goroutines exit, not only the output.
- Write a worker that stops when someone cancels its context.
- Run a slow function with a two second timeout and report the error.
- Cancel three workers when the first one finds a result.
- Add a request ID to the context in middleware and read it in the handler.
- Build a graceful shutdown with signal.NotifyContext and http.Server.Shutdown.
- Retry a call with backoff until the context deadline ends.
- Fetch three URLs with one shared deadline.
- Write a ticker loop that exits on ctx.Done.
- Use errgroup.WithContext to run tasks and stop on the first error.
- Test a timeout path without waiting for a real second.
- Wrap a blocking call that has no context support so that callers can give up.
- Scenario: when a search must return the fastest of three backends, how would you cancel the slower ones?
- Scenario: when a job must stop within five seconds of shutdown, how would you design its loop?
Code Reading Questions
Read each program carefully and predict the output before you run it. Then explain the result in your own words.
package main
import (
"context"
"errors"
"fmt"
"time"
)
func slowJob(ctx context.Context, d time.Duration) error {
select {
case <-time.After(d):
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
fmt.Println(slowJob(ctx, 10*time.Millisecond))
err := slowJob(ctx, time.Second)
fmt.Println(err)
fmt.Println(errors.Is(err, context.DeadlineExceeded))
child, stop := context.WithTimeout(ctx, time.Hour)
defer stop()
fmt.Println(child.Err())
}
- What do the four print lines show, and why does the second job stop early?
- Why is the child already done, even though it asks for a one hour timeout?
The second program mixes context values, a cancel cause and the context tree.
package main
import (
"context"
"errors"
"fmt"
)
type ctxKey string
const requestIDKey ctxKey = "requestID"
func handle(ctx context.Context) string {
id, ok := ctx.Value(requestIDKey).(string)
if !ok {
return "no id"
}
return "id=" + id
}
func main() {
base := context.WithValue(context.Background(), requestIDKey, "r-101")
fmt.Println(handle(base))
fmt.Println(handle(context.WithValue(base, "requestID", "r-999")))
fmt.Println(handle(context.Background()))
parent, cancel := context.WithCancelCause(base)
child, cancelChild := WithCancel(parent)
defer cancelChild()
cancel(errors.New("user logged out"))
<-child.Done()
fmt.Println(child.Err(), "|", context.Cause(child))
fmt.Println(handle(child))
}
- Why does the second line still print r-101 instead of r-999?
- What do the last two lines print, and how does the child see the cause from its parent?
Quick Revision Table for Go Context
Use the table below as a final check before your interview. It lists the context functions you will use most.
| Function | What it gives you | Typical use |
|---|---|---|
| context.Background | An empty root context | main, tests, top of a request |
| WithCancel | A child plus a cancel function | Stop workers on demand |
| WithTimeout | A child that ends after a time | Calls to other services |
| WithDeadline | A child that ends at a fixed time | Shared request budgets |
| WithValue | A child that carries one value | Request IDs and trace data |
| WithoutCancel | A child that ignores parent cancel | Work that must finish |
Also, many interviewers ask you to compare WithCancel and WithTimeout directly. The diagram below shows the difference side by side.

Official Resources
These official pages explain the rules behind these questions.
- Package context documentation
- Go blog: Go Concurrency Patterns, Context
- Go blog: Contexts and structs
- Canceling in-progress database operations
Quick Recap
- These Go context interview questions cover basics, cancellation, timeouts, values, real call chains and coding tasks.
- Pass ctx as the first parameter and never store it in a struct.
- Always call cancel, because it frees resources even when work succeeds.
- Cancelling a parent cancels every child, but never the other way round.
- A child cannot outlive its parent, so a longer child timeout has no effect.
- Keep context values for request data such as IDs, with unexported key types.
What’s Next?
By now, you have Go context interview questions for every part of the context package. Study the answers in your own notes, then check them against the Go interview topics study guide.
Next up: Go concurrency patterns interview questions (Part 18)


