Skip to content

Instantly share code, notes, and snippets.

@ostretsov
Created July 22, 2023 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ostretsov/1854e2848dbda7f79d10cd59551b0959 to your computer and use it in GitHub Desktop.
Save ostretsov/1854e2848dbda7f79d10cd59551b0959 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
)
func Example_parentCancelsChildren() {
parentCtx, cancelParentCtx := context.WithCancel(context.Background())
interimValCtx := context.WithValue(parentCtx, "foo", "bar")
childCtx, cancelChildCtx := context.WithCancel(interimValCtx)
defer cancelChildCtx()
defer cancelParentCtx()
cancelParentCtx()
fmt.Println("parent:", parentCtx.Err())
fmt.Println("interim:", interimValCtx.Err())
fmt.Println("child:", childCtx.Err())
// Output:
// parent: context canceled
// interim: context canceled
// child: context canceled
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment