Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
鉴于这种 :
var v reflect.Value = ... v.Type() // *model.Company
如何实例化一个新的 model.Company 并使用反射修改其字段?
类似于以下内容:
v := reflect.ValueOf(&Company{}) t := v.Type() c := reflect.New(t.Elem()).Elem() c.FieldByName("Name").SetString("Reflection Inc.") fmt.Printf("%#v\n", c.Interface()) // => main.Company{Name:"Reflection Inc."}
操场上的工作版本: