Talking about Golang reflect reflection calling function

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang also has the concept of reflection, in fact, most languages, especially that dynamic language have the concept of reflection and usage. In Python, getattr,globals () can restore the properties and methods of the object's bust by reflecting the introspection pattern.

Nonsense, what does reflection do? By reflecting we can restore a property of the image, method. Colloquially, in a distributed environment, I have sent you a JSON data structure, but this data structure is parsed to correspond to a method, this method can be used to reflect the call. In addition, Golang RPC is also implemented through reflection.

The article wrote a little messy, welcome to spray! In addition, the article continues to update, please go to the original address to view the update. http://xiaorui.cc/2016/03/14/%E6%B5%85%E8%B0%88golang-reflect%E5%8F%8D%E5%B0%84%E8%B0%83%E7%94%A8%E5%87%BD%E6%95%B0/

Golang implementation of the reflection is through the reflect package, so that the original static type of go with a lot of dynamic type language features. The reflect package has two data types, one is type and one is value.

Type is a data type of the defined type, value is the type of the values, typeof and ValueOf are methods to get the type and value.

The specific type and value contained in the method will look at the document:

http://golang.org/pkg/reflect/

The following is an example of a Golang code reflect reflection.

Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">#http://xiaorui.ccpackage mainimport ("FMT" "reflect") type Blog struct {Name string}func (this Blog) Test () string {FM T.println ("This is the Test method") return this. Name}func Main () {var o interface{} = &blog{"xiaorui.cc"}v: = Reflect. ValueOf (o) fmt. Println (v) m: = V.methodbyname ("Test") RETs: = M.call ([]reflect. value{}) fmt. Println (RETs) fmt. Println (Rets[0])}</textarea>
123456789101112131415161718192021222324252627 #http://xiaorui.cc PackageMainImport ("FMT""Reflect")type Blog struct {Namestring}func ( ThisBlog) Test() string {FMT.Println("This is Test method")return This.Name} funcMain() {var o Interface{} = &Blog{"xiaorui.cc"}v := reflect.ValueOf(o)FMT.Println(v)m := v.Methodbyname("Test")RETs := m.Pager([]reflect.Value{})FMT.Println(RETs)FMT.Println(RETs[0])}
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.