context.go

 1package config
 2
 3import "context"
 4
 5var ContextKeyConfig = &struct{ string }{"config"}
 6
 7// WithContext returns a new context with the configuration attached.
 8func WithContext(ctx context.Context, cfg *Config) context.Context {
 9	return context.WithValue(ctx, ContextKeyConfig, cfg)
10}
11
12// FromContext returns the configuration from the context.
13func FromContext(ctx context.Context) *Config {
14	if c, ok := ctx.Value(ContextKeyConfig).(*Config); ok {
15		return c
16	}
17
18	return DefaultConfig()
19}