《日子》golang-反射 reflection

直接上代码


package main
import (
"fmt"
"reflect"
)
type User struct {
Id int
Name string
Age int
}
func (u User) Hello() {
fmt.Println("Hello world.")
}
func main() {
u := User{1, "ok", 24}
Info(u)
}
func Info(o interface{}) {
t := reflect.TypeOf(o)
fmt.Println("Type:", t.Name())
if k := t.Kind(); k != reflect.Struct {
fmt.Println("XX")
return
}
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)
}
}

你可能感兴趣的:(《日子》golang-反射 reflection)