Skip to content

Instantly share code, notes, and snippets.

@maxwellgithinji
Created July 30, 2020 10:33
Show Gist options
  • Save maxwellgithinji/a252b5ce532330163b780197e8fa5926 to your computer and use it in GitHub Desktop.
Save maxwellgithinji/a252b5ce532330163b780197e8fa5926 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
)
type key int
var userKey key = 0
var ipKey key = 1
var isAdminKey key = 2
var sessionKey key = 3
func foo(ctx context.Context) {
ctx = context.WithValue(ctx, "currentFunc", "foo")
}
func main() {
ctx := context.WithValue(context.Background(), userKey, 1)
// ctx := context.Background()
userID, ok := ctx.Value(userKey).(int)
if !ok {
fmt.Println("Not Logged in!")
return
}
fmt.Println(userID)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment