我在路由器处理程序中有这段代码
decoder := json.NewDecoder(r.Body)
var t person.Model
err := decoder.Decode(&t). // t is a struct value
item, ok := v.People[params["id"]]. // ok is a struct value
if t.Handle != "" {
item.Handle = t.Handle
}
if t.Work != "" {
item.Work = t.Work
}
if t.Image != "" {
item.Image = t.Image
}
if t.Firstname != "" {
item.Firstname = t.Firstname
}
if t.Lastname != "" {
item.Lastname = t.Lastname
}
if t.Email != "" {
item.Email = t.Email
}
但我想让它变得动态,像这样:
["Handle", "Work", "Image", "Firstname", "Lastname", "Email"].forEach(v => {
if t[v] != "" {
item[v] = t[v]
}
});
这对 Golang 来说有可能吗?
万千封印
相关分类