Bluemage/go/config/context.go

21 lines
459 B
Go
Raw Permalink Normal View History

2024-08-11 19:50:43 +07:00
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
}