context.go 456 B

123456789101112131415161718192021
  1. package context
  2. import (
  3. gocontext "context"
  4. "golang.org/x/net/context"
  5. )
  6. const KEY = "context_key"
  7. type currentContextKey struct{}
  8. func WithCurrentContext(ctx gocontext.Context, contextName string) context.Context {
  9. return context.WithValue(ctx, currentContextKey{}, contextName)
  10. }
  11. // CurrentContext returns the current context name
  12. func CurrentContext(ctx context.Context) string {
  13. cc, _ := ctx.Value(currentContextKey{}).(string)
  14. return cc
  15. }