Skip to content

Instantly share code, notes, and snippets.

@marvinhosea
Created February 20, 2023 10:35
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 marvinhosea/bc0c47869a6af22afb52e339c2588644 to your computer and use it in GitHub Desktop.
Save marvinhosea/bc0c47869a6af22afb52e339c2588644 to your computer and use it in GitHub Desktop.
Contexpareet.go
package main
import (
"context"
"errors"
"log"
)
func main() {
parentCtx, parentCancelWithCause := context.WithCancelCause(context.Background())
childCtx, childCancelWithCause := context.WithCancelCause(parentCtx)
parentCancelWithCause(errors.New("error: parent canceled with cause"))
childCancelWithCause(nil)
<-parentCtx.Done()
<-childCtx.Done()
log.Println(parentCtx.Err())
log.Println(childCtx.Err())
log.Println(context.Cause(parentCtx))
log.Println(context.Cause(childCtx))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment