Go 反射

Submitted by Lizhe on Fri, 10/26/2018 - 03:23

package main

 

import (

    "fmt"

    "reflect"

)

 

type User struct {

    Id int

    Name string

    Age int

}

 

func (u User) Hello() {

    fmt.Println("Hello world!")

}

func Info(o interface{}) {

    t := reflect.TypeOf(o)

    fmt.Println("Type:", t.Name())

 

    v := reflect.ValueOf(o)

    fmt.Println("Fields:")

    for i := 0; i < t.NumField(); i++ {

        f := t.Field(i)

        val := v.Field(i).Interface()

        fmt.Printf("%6s:%v =%v\n", f.Name, f.Type, val)

    }

    for i := 0; i < t.NumMethod(); i++ {

        m := t.Method(i)

        fmt.Printf("%6s:%v\n", m.Name, m.Type)

 

    }

}

func main() {

    u := User{1, "Jack", 23}

    Info(u)

}