本文整理汇总了Golang中context.Context.Value方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Value方法的具体用法?Golang Context.Value怎么用?Golang Context.Value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类context.Context
的用法示例。
在下文中一共展示了Context.Value方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: InboundDestinationFromContext
func InboundDestinationFromContext(ctx context.Context) net.Destination {
v := ctx.Value(inboundDestinationKey)
if v == nil {
return net.Destination{}
}
return v.(net.Destination)
}
示例2: UserFromContext
// UserFromContext returns a user from a context.Context if one is present.
func UserFromContext(ctx context.Context) *empire.User {
u, ok := ctx.Value(userKey).(*empire.User)
if !ok {
panic("expected user to be authenticated")
}
return u
}
示例3: FromContextOrNil
func FromContextOrNil(ctx context.Context) *App {
e, ok := ctx.Value(ctxKey).(*App)
if ok {
return e
}
return nil
}
示例4: FromContext
// FromContext returns the User value stored in ctx, if any.
func FromContext(ctx context.Context) *Cmd {
e, ok := ctx.Value(cmdKey).(*Cmd)
if !ok {
panic(kerr.New("OQVLBQFQJW", "No cmd in ctx").Error())
}
return e
}
示例5: dialSingle
// dialSingle attempts to establish and returns a single connection to
// the destination address.
func dialSingle(ctx context.Context, dp *dialParam, ra Addr) (c Conn, err error) {
trace, _ := ctx.Value(nettrace.TraceKey{}).(*nettrace.Trace)
if trace != nil {
raStr := ra.String()
if trace.ConnectStart != nil {
trace.ConnectStart(dp.network, raStr)
}
if trace.ConnectDone != nil {
defer func() { trace.ConnectDone(dp.network, raStr, err) }()
}
}
la := dp.LocalAddr
switch ra := ra.(type) {
case *TCPAddr:
la, _ := la.(*TCPAddr)
c, err = dialTCP(ctx, dp.network, la, ra)
case *UDPAddr:
la, _ := la.(*UDPAddr)
c, err = dialUDP(ctx, dp.network, la, ra)
case *IPAddr:
la, _ := la.(*IPAddr)
c, err = dialIP(ctx, dp.network, la, ra)
case *UnixAddr:
la, _ := la.(*UnixAddr)
c, err = dialUnix(ctx, dp.network, la, ra)
default:
return nil, &OpError{Op: "dial", Net: dp.network, Source: la, Addr: ra, Err: &AddrError{Err: "unexpected address type", Addr: dp.address}}
}
if err != nil {
return nil, &OpError{Op: "dial", Net: dp.network, Source: la, Addr: ra, Err: err} // c is non-nil interface containing nil pointer
}
return c, nil
}
示例6: GetPoster
func GetPoster(ctx context.Context) Poster {
poster := ctx.Value(posterKey{})
if poster == nil {
logger := log.G(ctx)
tx, _ := getTx(ctx)
topic := getTopic(ctx)
// likely means we don't have a configured event system. Just return
// the default poster, which merely logs events.
return posterFunc(func(ctx context.Context, event Event) {
fields := logrus.Fields{"event": event}
if topic != "" {
fields["topic"] = topic
}
if tx != nil {
fields["tx.id"] = tx.id
if tx.parent != nil {
fields["tx.parent.id"] = tx.parent.id
}
}
logger.WithFields(fields).Info("event posted")
})
}
return poster.(Poster)
}
示例7: ExtractUser
// ExtractUser extracts user from from context.
func ExtractUser(ctx context.Context) (*User, error) {
u, ok := ctx.Value(userKey).(*User)
if !ok {
return nil, errors.New("not found context user")
}
return u, nil
}
示例8: FromContextOrNil
func FromContextOrNil(ctx context.Context) *sync.WaitGroup {
wg, ok := ctx.Value(wgKey).(*sync.WaitGroup)
if !ok {
return nil
}
return wg
}
示例9: FromContext
// FromContext returns the Cache value stored in ctx, and panics if it's not found.
func FromContext(ctx context.Context) *JsonCache {
e, ok := ctx.Value(jsonKey).(*JsonCache)
if !ok {
panic(kerr.New("XUTUUVDMMX", "No json cache in ctx").Error())
}
return e
}
示例10: Handler
/*
Handler returns the handler corresponding to the most recently matched Pattern,
or nil if no pattern was matched.
The handler returned by this function is the one that will be dispatched to at
the end of the middleware stack. If the returned Handler is nil, http.NotFound
will be used instead.
*/
func Handler(ctx context.Context) http.Handler {
h := ctx.Value(internal.Handler)
if h == nil {
return nil
}
return h.(http.Handler)
}
示例11: FromContext
// FromContext returns the User value stored in ctx, if any.
func FromContext(ctx context.Context) *sync.WaitGroup {
wg, ok := ctx.Value(wgKey).(*sync.WaitGroup)
if !ok {
panic(kerr.New("UNYGADKEGY", "No wg in ctx").Error())
}
return wg
}
示例12: Pattern
/*
Pattern returns the most recently matched Pattern, or nil if no pattern was
matched.
*/
func Pattern(ctx context.Context) goji.Pattern {
p := ctx.Value(internal.Pattern)
if p == nil {
return nil
}
return p.(goji.Pattern)
}
示例13: ResponseTypeFromCtx
// ResponseTypeFromCtx gives the content type that was parsed from the
// 'Accept' header.
func ResponseTypeFromCtx(ctx context.Context) *ContentType {
ct := ctx.Value(httpware.ResponseContentTypeKey)
if ct == nil {
return nil
}
return ct.(*ContentType)
}
示例14: OutboundTagFromContext
func OutboundTagFromContext(ctx context.Context) string {
v := ctx.Value(outboundTagKey)
if v == nil {
return ""
}
return v.(string)
}
示例15: ParamsFromContext
// ParamsFromContext - get the current params value from a given context
func ParamsFromContext(ctx context.Context) url.Values {
params := ctx.Value("params")
if params != nil {
return url.Values(params.(map[string][]string))
}
return nil
}