• 疯琴
    2019-11-22
    请问老师,为什么.FieldByName前面要用reflect.ValueOf(*e)而MethodByName前面要用reflect.ValueOf(e),一个是值一个是指针。另外reflect.TypeOf(e).MethodByName("UpdateAge")为什么没有Call方法了?
    
    
  • blackpiglet
    2019-09-16
    改了好几遍程序,终于明白为什么要写成这样了, e := &Employee{1, "Mike", 30}
    原因是:
    1. UpdateAge 必须得声明为 e *Employee 类型的成员变量才能将修改的值保存;
    2. Call 调用 UpdateAge 就必须得传入 *Employee 类型的变量才能调用成功。
    
    
  • Hao
    2019-09-05
    reflect.ValueOf(<pointer type>).MethodByName()
    reflect.ValueOf(<non-pointer type>).FieldByName()

    Why does MethodByName requires a pointer type while FieldByName not? Is there any reason about it?
    
    
我们在线,来聊聊吧