"Golang" Golang how do you reflect an object by its class name like Java?

Source: Internet
Author: User

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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.