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