FromContext returns value from the routing context stored in ctx. It returns value associated with the key or stores result of the defaultValue call. defaultValue may be called multiple times but only one result will be used as a default value.
(ctx context.Context, key K, defaultValue func() V)
| 22 | // It returns value associated with the key or stores result of the defaultValue call. |
| 23 | // defaultValue may be called multiple times but only one result will be used as a default value. |
| 24 | func FromContext[K comparable, V any](ctx context.Context, key K, defaultValue func() V) V { |
| 25 | m, _ := ctx.Value(routingContextKey).(*sync.Map) |
| 26 | |
| 27 | // https://github.com/golang/go/issues/44159#issuecomment-780774977 |
| 28 | val, ok := m.Load(key) |
| 29 | if !ok { |
| 30 | val = defaultValue() |
| 31 | val, _ = m.LoadOrStore(key, val) |
| 32 | } |
| 33 | return val.(V) |
| 34 | } |
no outgoing calls
searching dependent graphs…