Use of Go language structure reflection

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

Reflect. Value distinguishes between Canset and can not set, so it must be returned to the reflect of can set. Value
Such as:

S: = reflect. ValueOf (&t). Elem ()

Then you can set the value of the happy, but not arbitrarily set the value, a common method is to use the set (V Value) method, so the value must be converted to reflect. Value type, the following code is the one that goes to the value type

Slicevalue: = reflect. ValueOf ([]int{1, 2, 3})//turn slice into reflect here. Value type

Full reference:

Type T struct {age int Name string children []int}t: = t{12, "Someone-life", nil}s: = reflect. ValueOf (&t). Elem () S.field (0). Setint (123)//built-in common type set Value method Slicevalue: = Reflect. ValueOf ([]int{1, 2, 3})//turn slice into reflect here. The value type S.fieldbyname ("Children"). Set (Slicevalue)

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.