21 lines
459 B
Go
21 lines
459 B
Go
package config
|
|
|
|
import "context"
|
|
|
|
type contextKey struct{}
|
|
|
|
var key = contextKey{}
|
|
|
|
// WithContext returns a new context with the given Config.
|
|
func WithContext(ctx context.Context, cfg *Config) context.Context {
|
|
return context.WithValue(ctx, key, cfg)
|
|
}
|
|
|
|
// FromContext returns the Config from the given context.
|
|
//
|
|
// Returns nil if the Config is not found.
|
|
func FromContext(ctx context.Context) *Config {
|
|
cfg, _ := ctx.Value(key).(*Config)
|
|
return cfg
|
|
}
|