Created
July 30, 2020 10:33
-
-
Save maxwellgithinji/a252b5ce532330163b780197e8fa5926 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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