Go reflect 3 rules

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


1. Reflection goes from interface value to Reflection Object.

Reflection can be worthwhile from the interface to the reflective object

Use to reflect methods:

Reflect. ValueOf (i interface{}) Value

Reflect. TypeOf (i interface{}) Type


2. Reflection goes from Refelction object to interface value.

Reflection can get interface values from reflected objects

Use to reflect methods:

Reflect. New (Typ Type) Value

Reflect. Zero (Typ Type) Value

Func (v Value) type () type
Func (v Value) Interface () (I interface{})

I. (SOMEINTERFACE)


3. To modify a reflection object, the value must is settable.

To modify a reflection object, its value must be modifiable


Finally add a: type and value of mutual conversion and auxiliary methods

Type->value

Reflect. New (Typ Type) Value


Value->type

Func (v Value) type () type


Pointervalue->value

Func Indirect (v value) value

Func (v Value) Elem () value


Type->pointertype

Func Ptrto (t type) type


Pointertype->type

Func (t type) Elem () type


Note: Go's reflection is not powerful enough, it's different directly from the Java, it is created directly through the class package string, namely: its different through the type name directly constructs type or value, you must first create a type variable, and then the type or value variable. But this can be solved by adding a syntactic sugar.









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.