Use context.Background() for top-level contexts and context.TODO() as a temporary placeholder when the correct context is unknown.
Use context.Background() to start a new context at the top of your program, and context.TODO() as a placeholder when you don't yet know which context to use.
// Start at the top level
ctx := context.Background()
// Placeholder for later implementation
func handler(ctx context.Context) {
// TODO: replace with proper context
_ = context.TODO()
}
Think of context.Background() as the root of your program's execution tree, used when you have no other context to pass. Use context.TODO() as a temporary placeholder when you are writing code that requires a context but haven't decided which one to use yet. It prevents compilation errors while you refine your logic.