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."}
プレイグラウンドでの作業バージョン: