Set a pointer to a field using reflection

前端 未结 1 966
暖寄归人
暖寄归人 2021-01-27 17:13

i have the following struct, and need some of the fields to be nulluble so i use pointers, mainly to handle sql nulls

type Chicken struct{
    Id                         


        
相关标签:
1条回答
  • 2021-01-27 17:53

    reflect.Value.Set only accepts reflect.Value as an argument. Use reflect.ValueOf on your stringValue:

    item.Elem().FieldByName("Name").Set(reflect.ValueOf(&stringValue))
    

    Playground: http://play.golang.org/p/DNxsbCsKZA.

    0 讨论(0)
提交回复
热议问题