a8m / a8m/reflect-examples

Use reflection to dump context

未关闭 适合新手
#6 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
没有语言数据
星标
568
派生
73
PR 合并指标
30 天内没有已合并 PR

描述

Sometimes for local debugging, using [reflection to dump the context](https://stackoverflow.com/a/60278058/2904179) is handy:
```
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")
}
}
```

Thanks for putting all your examples in one central place!

贡献指南

这个仓库没有索引到贡献指南

调研方向

该 issue 建议为 Go context 添加一个基于 reflection 的调试工具。查看 repository 中现有的示例,以了解其结构和命名方式。可以将提供的函数调整到一个新的示例文件中,该文件可能位于名为 'context' 或 'debug' 的目录中。确保示例能够编译和运行,并演示如何检查 context 的内部实现。检查是否已有测试,以了解示例是如何验证的。

由索引模型根据 Issue 内容生成。

评估

技术栈
go
领域
documentation
Issue 类型
文档
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
75/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。