go - Go Reflection - 带有方法的动态类型
问题描述
使用 Go 的包,您可以使用reflect.StructOf()reflect
创建动态结构类型:
typ := reflect.StructOf([]reflect.StructField{
{
Name: "Height",
Type: reflect.TypeOf(float64(0)),
Tag: `json:"height"`,
},
{
Name: "Age",
Type: reflect.TypeOf(int(0)),
Tag: `json:"age"`,
},
})
有没有办法创建将这种新的动态类型作为接收器的函数/方法?具体来说,我想创建一个动态类型,使用StructOf()
它实现一个接口(定义了几个方法),这样reflect.New(typ).Interface().(InterfaceType)
会成功。
解决方案
推荐阅读
- c# - Qna 和 LUIS 在下一步处理输入之前中断对话框
- python - 在单个查询中获取所有顶点和边作为地图
- javascript - 在nestjs中使用类验证器验证可选参数?
- linux - VSCode 远程 Python 虚拟环境
- asp.net - Visual Studio 2019 右键单击 WebForms 缺少 Go To Definition
- android - java.lang.NoClassDefFoundError:解析失败:Ldji/common/flightcontroller/CompassCalibrationState
- php - 每个循环PHP的多输入和多列
- ibm-cloud - IBM Watson Assistant:如何解决 web_action 错误“内部错误:无法检索内容类型。”
- ios - Firebase 配置问题:Firebase Analytics 不可用
- typescript - 如何在 Jest 的 globalSetup 函数中使用模块导入 (@) 和 TypeScript?