Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Go: How to use context value in a variable?

I am using context to attach user payload(userId specifically) in a middleware for my go rest application

// middleware
    // attaching payload to the request context
        claimsWithPayload, _ := token.Claims.(*handlers.Claims)
        ctx := context.WithValue(r.Context(), "userid", claimsWithPayload.Id)
        req := r.WithContext(ctx)
        h := http.HandlerFunc(handler)
        h.ServeHTTP(w, req)

And later in a http handler, I need to extract that userid as string/integer
but context().Value() returns an interface{}

// handler 
a := r.Context().Value("userid") // THIS returns an interface{}
response := []byte("Your user ID is" + a) // 👈 how do I use it as a string/integer??
w.Write(response)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a type assertion to get the context value as its underlying type:

a := r.Context().Value("userid").(string)

This will panic if the value stored by the middleware is not a string, or if something else sets the context key to something which is not a string. To guard against this you should never use builtin types as context keys, instead define your own type and use that:

type contextKey string
const userIDKey contextKey = "userid"
...
ctx := context.WithValue(r.Context(), userIDKey, claimsWithPayload.Id)
...
a := r.Context().Value(userIDKey).(string)

Because contextKey and userIDKey are unexported, only your package can read or write this value from or to the context.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading