Standard library / context
context.WithoutCancel()
Detach cancellation and deadline from the parent while retaining values. Done is nil. Supply a new explicit bound for detached work. Expected stdout: 'demo true true false\n'.
Formal Syntax & Structure
go syntaxgo
func WithoutCancel(parent Context) ContextParameters & Attributes
| Name | Type | Description |
|---|---|---|
| parent | Context | Non-nil parent context. |
Return Value
Context
Context retaining parent values but not cancellation or deadline.
Working Example
Example Snippetgo
package main
import ( "context" "fmt")
func main() { type key struct{} p, cancel := context.WithCancel(context.WithValue(context.Background(), key{}, "demo")) defer cancel() ctx := context.WithoutCancel(p) cancel() _, ok := ctx.Deadline() fmt.Println(ctx.Value(key{}), ctx.Done() == nil, ctx.Err() == nil, ok)}