Skip to content

Instantly share code, notes, and snippets.

@marvinhosea
Last active February 20, 2023 20:19
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/625001d267eeadc1cc60872aceccdc51 to your computer and use it in GitHub Desktop.
Save marvinhosea/625001d267eeadc1cc60872aceccdc51 to your computer and use it in GitHub Desktop.
Child context.go
package main
import (
"context"
"errors"
"log"
)
func main() {
parentCtx, parentCancelWithCause := context.WithCancelCause(context.Background())
childCtx, childCancelWithCause := context.WithCancelCause(parentCtx)
childCancelWithCause(errors.New("error: child canceled with cause"))
parentCancelWithCause(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