假设我有一个结构用户
type User struct {
Name string `owm:"newNameFromAPI"`
}
下面的代码初始化结构并将其传递给函数
func main() {
dest := User{
Name: "Sebastien",
}
deepUpdate(dest, "owm")
}
该函数使用反射来迭代结构的所有字段并相应地更新它们(为清楚起见,删除了几个代码块)
func deepUpdate(destinationInterface interface{}, selector string) {
// Here I'm not supposed to know that the type is `User`
// And if I just use dest := destinationInterface, the value won't update
dest := destinationInterface.(User)
// Pointer to struct - addressable
destVal := reflect.ValueOf(&dest)
destType := reflect.TypeOf(&dest)
// psindirect := reflect.Indirect(destVal) // ? ValueOf(<Ptr Value>) Requires to be adressed via reflect.Indirect() to access Field - https://stackoverflow.com/a/50098755/9077800
// Struct
destValElem := destVal.Elem()
destTypeElem := destType.Elem()
// Iterate over all fields of the struct
for i := 0; i < destValElem.NumField(); i++ {
// // for i := 0; i < destTypeElem.NumField(); i++ {
destValField := destValElem.Field(i)
destTypeField := destTypeElem.Field(i)
switch destValField.Kind() {
// Field is a struct
case reflect.Struct:
// Recursion
fmt.Println("IS A STRUCT")
default:
// Get the complete tag
tag := destTypeField.Tag
if len(tag) == 0 {
continue
}
// Get the value of our custom tag key
tagVal := tag.Get(selector)
if len(tagVal) == 0 {
continue
}
}
问题是以下行,因为我不应该知道要将 转换为 .destinationInterfaceUser
如何将接口动态转换为在运行时定义的某个未知类型,或者以任何其他方式获取更新的“John”而不是“Sebastien”的相同输出?Name
dest := destinationInterface.(User)
这是在戈朗游戏狗上运行的完整代码
https://play.golang.org/p/sYvz-Fwp97P
至尊宝的传说
相关分类