Golang Reflex Reflect

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

Reflect reflection, can be reflected by reflect the properties and methods contained in the structure, and then make some assignment and method calls, flexibility is higher

package myimport (    ft "fmt"    "reflect")type User struct {    Id   int    Name string}func (user User) Print() {    ft.Println("reflect Print()")}func Reflect(inter interface{}) {    t := reflect.TypeOf(inter) //从接口中获取结构的类型    if k:=t.Kind();k!=reflect.Struct{//判断传入的是否是struce类型,而不是指针类型*User,指针类型报错        ft.Println("type is not true")        return    }    ft.Println("类型名称:", t.Name())    v := reflect.ValueOf(inter) //从接口中获取结构的值    for i := 0; i < t.NumField(); i++ { //遍历所包含的属性字段        f := t.Field(i) //获取到字段        val := v.Field(i).Interface()        ft.Println("字段签名:", f.Type, " 字段名称:", f.Name, "  值:", val)    }    for i := 0; i < t.NumMethod(); i++ { //遍历所绑定的方法        m := t.Method(i) //获取到方法        ft.Println("方法名称:", m.Name, " 方法签名:", m.Type)    }}
    • Reflection of structure nesting
 Package Myimport (FT "FMT" "reflect") type User struct {Id int Name string Info}type Info struct { Age Int}func (User user) Print (name string) {ft. Println ("Reflect Print ()", name)}func reflect (inter interface{}) {t: = reflect. TypeOf (inter)//Gets the type of structure from the interface V: = reflect. ValueOf (inter)//Gets the value of the structure from the interface if t.kind () = = reflect. Ptr && V.elem (). Canset () {///is passed in a pointer that can be modified by V = V.elem () if f: = V.kind (); f = = reflect. struct {//If the field property is struct if x: = V.fieldbyname ("Age"); X.isvalid () {x.setint (888)} If f: = V.fieldbyname ("Name"); F.kind () = = reflect. String && F.isvalid () {f.setstring ("haha")} if f: = V.fieldbyname ("Id"); F.kind () = = reflect. Int && F.isvalid () {f.setint ()} if f: = V.methodbyname ("Print"); F.isvalid () {args: = []reflect. Value{reflect. ValueOf ("Test")} f.call (args)}}} 
  Package Mainimport (f "FMT" "com.guo/mytest/something") func main () {r:=something. User{1, "haha", something. info{100, "Guangzhou"}} something. Reflect (&r) something. Reflect (R) f.println ("Modified result:", R)}  

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.