我有以下结构:
type ProductionInfo struct {
StructA []struct {
Field1 string
Field2 int
}
我将从 ProductionInfo 类型的 StructA 中提取字段名称和类型。但我不明白如何。谁能帮帮我?
最佳答案
使用反射包:
f, _ := reflect.TypeOf(ProductionInfo{}).FieldByName("StructA")
t := f.Type.Elem()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
fmt.Println(f.Name, f.Type)
}
关于Golang 反射(reflect)在 slice 中获取结构成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44214809/