env.go 447 B

1234567891011121314151617181920
  1. package envctx
  2. import "context"
  3. type environmentContextKey int
  4. const (
  5. environmentKey environmentContextKey = iota
  6. )
  7. func ContextWithEnvironment(ctx context.Context, environment interface{}) context.Context {
  8. return context.WithValue(ctx, environmentKey, environment)
  9. }
  10. func EnvironmentFromContext(ctx context.Context) interface{} {
  11. if environment, ok := ctx.Value(environmentKey).(interface{}); ok {
  12. return environment
  13. }
  14. return nil
  15. }