context.Context にキーをリストする方法はありますか? 質問する

context.Context にキーをリストする方法はありますか? 質問する

つまり、コンテキストがあります。コンテキスト(https://golang.org/pkg/context/) 変数を私と一緒に使用したいのですが、この変数が保持するすべてのキーを一覧表示する方法はありますか?

ベストアンサー1

安全でないリフレクションを使用して context.Context の内部を一覧表示し、その情報を使用してキーを把握したり、必要な情報がコンテキスト内にあるかどうかを確認したりすることができます。

コンテキスト実装がキーに対してハードコードされた値を返す場合、その値はここには表示されないなど、いくつかの落とし穴があり、キーを使用して実際に値にアクセスする方法がかなり不明確になる可能性があります。

これは本番環境で実行するものではありません。しかし、私の場合は、context.Context を検査して、そこにどのような情報が含まれているかをよりよく理解できるようにする必要がありました。

func printContextInternals(ctx interface{}, inner bool) {
    contextValues := reflect.ValueOf(ctx).Elem()
    contextKeys := reflect.TypeOf(ctx).Elem()

    if !inner {
        fmt.Printf("\nFields for %s.%s\n", contextKeys.PkgPath(), contextKeys.Name())
    }

    if contextKeys.Kind() == reflect.Struct {
        for i := 0; i < contextValues.NumField(); i++ {
            reflectValue := contextValues.Field(i)
            reflectValue = reflect.NewAt(reflectValue.Type(), unsafe.Pointer(reflectValue.UnsafeAddr())).Elem()

            reflectField := contextKeys.Field(i)

            if reflectField.Name == "Context" {
                printContextInternals(reflectValue.Interface(), true)
            } else {
                fmt.Printf("field name: %+v\n", reflectField.Name)
                fmt.Printf("value: %+v\n", reflectValue.Interface())
            }
        }
    } else {
        fmt.Printf("context is empty (int)\n")
    }
}

例:

func Ping(w http.ResponseWriter, r *http.Request) {
    printContextInternals(r.Context(), false)
    /* Prints
        Fields for context.valueCtx
        context is empty (int)
        field name: key
        value: net/http context value http-server
        field name: val
        value: &{Addr::20885 Handler:0xc00001c000 TLSConfig:0xc000001c80 ReadTimeout:0s ReadHeaderTimeout:0s WriteTimeout:0s IdleTimeout:0s MaxHeaderBytes:0 TLSNextProto:map[h2:0x12db010] ConnState:<nil> ErrorLog:<nil> BaseContext:<nil> ConnContext:<nil> disableKeepAlives:0 inShutdown:0 nextProtoOnce:{done:1 m:{state:0 sema:0}} nextProtoErr:<nil> mu:{state:0 sema:0} listeners:map[0xc00015a840:{}] activeConn:map[0xc000556fa0:{}] doneChan:<nil> onShutdown:[0x12e9670]}
        field name: key
        value: net/http context value local-addr
        field name: val
        value: [::1]:20885
        field name: mu
        value: {state:0 sema:0}
        field name: done
        value: 0xc00003c2a0
        field name: children
        value: map[context.Background.WithValue(type *http.contextKey, val <not Stringer>).WithValue(type *http.contextKey, val [::1]:20885).WithCancel.WithCancel:{}]
        field name: err
        value: <nil>
        field name: mu
        value: {state:0 sema:0}
        field name: done
        value: <nil>
        field name: children
        value: map[]
        field name: err
        value: <nil>
        field name: key
        value: 0
        field name: val
        value: map[]
        field name: key
        value: 1
        field name: val
        value: &{handler:0x151cf50 buildOnly:false name: err:<nil> namedRoutes:map[] routeConf:{useEncodedPath:false strictSlash:false skipClean:false regexp:{host:<nil> path:0xc0003d78f0 queries:[]} matchers:[0xc0003d78f0 [GET POST]] buildScheme: buildVarsFunc:<nil>}}

    */


    printContextInternals(context.Background(), false)
    /* Prints
        Fields for context.emptyCtx
        context is empty (int)
    */
}

おすすめ記事