Lab 07: Context Package
Overview
Step 1: context.Background and context.TODO
package main
import (
"context"
"fmt"
)
func main() {
// Use Background for main, init, tests, and top-level contexts
bg := context.Background()
fmt.Println("background:", bg)
// Use TODO as a placeholder when unsure which context to use
todo := context.TODO()
fmt.Println("todo:", todo)
}Step 2: WithCancel — Manual Cancellation
Step 3: WithTimeout — Deadline After Duration
Step 4: WithDeadline — Absolute Time Limit
Step 5: WithValue — Request-Scoped Values
Step 6: Propagating Cancellation Through Layers
Step 7: Context in Goroutine Cleanup
Step 8: Capstone — HTTP Handler with Context
Summary
Function
Use Case
Key Behaviour
Last updated
