Standard library / context
context.Cause()
Return the first recorded cancellation cause, nil before cancellation. A later cancel cannot overwrite it. Expected stdout: 'first\n'.
Formal Syntax & Structure
go syntaxgo
func Cause(c Context) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| c | Context | Context to inspect for its cancellation cause. |
Return Value
error
Cancellation cause or nil.
Working Example
Example Snippetgo
package main
import ( "context" "errors" "fmt")
func main() { ctx, cancel := context.WithCancelCause(context.Background()) defer cancel(nil) cancel(errors.New("first")) cancel(errors.New("second")) fmt.Println(context.Cause(ctx))}