我正在尝试在Go中创建一个通用方法,它将填充struct来自a 的使用数据map[string]interface{}.例如,方法签名和用法可能如下所示:
func FillStruct(data map[string]interface{}, result interface{}) {
...
}
type MyStruct struct {
Name string
Age int64
}
myData := make(map[string]interface{})
myData["Name"] = "Tony"
myData["Age"] = 23
result := &MyStruct{}
FillStruct(myData, result)
// result now has Name set to "Tony" and Age set to 23
Run Code Online (Sandbox Code Playgroud)
我知道这可以使用JSON作为中介来完成; 还有另一种更有效的方法吗?
go ×1