Created
July 22, 2023 03:12
-
-
Save ostretsov/1854e2848dbda7f79d10cd59551b0959 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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