Conclusion:
golang不支持解析string然后执行。
golang的反射机制只能存在于已经存在的对象上面。
It is not known whether the subsequent versions are planned, and now only the registration can be loaded, and then a reflection similar to the Java Factory pattern is implemented.
code example:
T: = reflect. ValueOf (human{}). Type () // H: = reflect. New (t). Elem () // New return address pointer h: = reflect. New (t). Interface () FMT. Println (h) hh:= H. (*Human) FMT. Println (HH) HH. SayHello () 123 "ABC " 345 HH. SayHello ()
i = human{"Emp", 25, 120}
Fmt. Println (reflect. TypeOf (i). Field (0). Type)
Fmt. Println (reflect. ValueOf (i). Field (1))
Reflect. ValueOf (i). Field (1). Elem (). Setint (88)
Fmt. Println (reflect. ValueOf (i). Field (1))
Resources:
Know why you can't create a Golang object from a string: https://www.zhihu.com/question/25580049
Http://www.tuicool.com/articles/ZJBNni
https://my.oschina.net/wujibing/blog/682802
http://studygolang.com/articles/896
http://blog.csdn.net/rufidmx/article/details/18226649
Http://www.cnblogs.com/yjf512/archive/2012/06/10/2544391.html
"Golang" Golang how do you reflect an object by its class name like Java?