context.go

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