Home > front end >  How to store a value of type context.CancelFunc using context.WithValue()?
How to store a value of type context.CancelFunc using context.WithValue()?

Time:10-06

To store string type data in ctx, type definitions need to be used for both key & value, as shown below:

    // Sample program to show how to store and retrieve
    // values from a context.
    package main
    
    import (
        "context"
        "fmt"
    )
    
    // TraceID represents the trace id.
    type TraceID string
    
    // TraceIDKey is the type of value to use for the key. The key is
    // type specific and only values of the same type will match.
    type TraceIDKey int
    
    func main() {
    
        // Create a traceID for this request.
        traceID := TraceID("f47ac10b-58cc-0372-8567-0e02b2c3d479")
    
        // Declare a key with the value of zero of type userKey.
        const traceIDKey TraceIDKey = 0
    
        // Store the traceID value inside the context with a value of
        // zero for the key type.
        ctx := context.WithValue(context.Background(), traceIDKey, traceID)
    
        // Retrieve that traceID value from the Context value bag.
        if uuid, ok := ctx.Value(traceIDKey).(TraceID); ok {
            fmt.Println("TraceID:", uuid)
        }
    
        // Retrieve that traceID value from the Context value bag not
        // using the proper key type.
        if _, ok := ctx.Value(0).(TraceID); !ok {
            fmt.Println("TraceID Not Found")
        }
    }

How to store a value of type context.CancelFunc using context.WithValue() api?

CodePudding user response:

You are partially correct. You should use a bespoke type for your context key rather than the builtin types to make collisions impossible. This type should be unexported unless you want other packages to be able to read/write your context key. However the value can be anything you like, eg:

package main

import (
    "context"
    "fmt"
)

type contextKey int

const (
    traceIDKey contextKey = iota
    aFunctionWhyNot
)

func main() {

    // Create a traceID for this request.
    traceID := "f47ac10b-58cc-0372-8567-0e02b2c3d479"

    // Store the traceID value inside the context with a value of
    // zero for the key type.
    ctx := context.WithValue(context.Background(), traceIDKey, traceID)

    // Retrieve that traceID value from the Context value bag.
    if uuid, ok := ctx.Value(traceIDKey).(string); ok {
        fmt.Println("TraceID:", uuid)
    }

    // Or a function
    ctx = context.WithValue(ctx, aFunctionWhyNot, func() { fmt.Println("lol, I'm a function on a context") })

    // Call it maybe
    if f, ok := ctx.Value(aFunctionWhyNot).(func()); ok {
        f()
    }
}

CodePudding user response:

You can store a function in the context the same way you store any other value:

type cancelFuncKeyType struct{}
var cancelFuncKey =cancelFuncKeyType{}

...
newctx:=context.WithValue(oldctx,cancelFuncKey,cancelFunc)


cFunc:=newctx.Value(cancelFuncKey).(context.CancelFunc)
  • Related