go 反射 遍历对象属性 切片 Map

package main

import "fmt"
import "reflect"

func main() {
    p1 := Person{Name: "test1", Age: 20, Address: "1323"}
    p2 := Person{Name: "demo2", Age: 24, Address: "adsd"}
    var list []*Person
    list = append(list, &p1)
    list = append(list, &p2)

    var list2 []Person
    list2 = append(list2, p1)
    list2 = append(list2, p2)

    res := &ResponseData{ID: 1, Remark: "test", Data: list, List: list, List2: list2, Data2: list2, List3: []int64{1, 2, 3, 4}}

    res.Data4 = make(map[string]*Person)
    res.Data4[p1.Name] = &p1
    res.Data4[p2.Name] = &p2
    res.Data5 = make(map[int]Person)
    res.Data5[p1.Age] = p1
    res.Data5[p2.Age] = p2

    m := map[string]int{"test1": 1, "test2": 2}
    res.Data6 = m

    logResponse(res)
}

type Person struct {
    Name    string
    Age     int
    Address string
}

type ResponseData struct {
    ID     int         `json:"id"`
    Remark string      `json:"remark"`
    Data   []*Person   `json:"data"`
    List   interface{} `json:"list"`
    Data2  []Person    `json:"data2"`
    List2  interface{} `json:"list2"`
    List3  interface{} `json:"list3"`
    Data4  map[string]*Person
    Data5  map[int]Person
    Data6  map[string]int
}

func logResponse(obj interface{}) {

    defer func() {
        if e := recover(); e != nil {
            fmt.Printf("Panicing %s\r\n", e)
        }
    }()

    if obj == nil {
        return
    }

    t := reflect.TypeOf(obj)
    v := reflect.ValueOf(obj)

    k := v.Kind()
    if k == reflect.Ptr {
        v = v.Elem()
        t = v.Type()
        k = v.Kind()
    }

    if k != reflect.Struct {
        return
    }

    num := v.NumField()
    for i := 0; i < num; i++ {
        name := t.Field(i).Name
        f := v.Field(i)

        isSlice := false
        isMap := false
        if f.Kind() == reflect.Slice {
            isSlice = true
        } else if f.Kind() == reflect.Map {
            isMap = true
        } else if f.Kind() == reflect.Interface && f.CanInterface() {
            f = f.Elem()
            if f.Kind() == reflect.Slice {
                isSlice = true
            } else if f.Kind() == reflect.Map {
                isMap = true
            }
        }

        if isSlice {
            c := f.Len()
            for j := 0; j < c; j++ {
                vl := f.Index(j)
                if vl.Kind() == reflect.Ptr {
                    vl = vl.Elem()
                }

                if vl.Kind() == reflect.Struct {
                    fmt.Printf("%s[%d]=%+v \n", name, j, vl)
                } else {
                    fmt.Printf("%s %s = %v -tag:%s \n", name, t.Field(i).Type, f.Interface(), t.Field(i).Tag)
                    break
                }

            }
        } else if isMap {
            for _, key := range f.MapKeys() {
                val := f.MapIndex(key)
                if val.Kind() == reflect.Ptr {
                    val = val.Elem()
                }
                if val.Kind() == reflect.Struct {
                    fmt.Printf("%s[%v]=%+v \n", name, key.Interface(), val)
                } else {
                    fmt.Printf("%s %s = %v -tag:%s \n", name, t.Field(i).Type, f.Interface(), t.Field(i).Tag)
                    break
                }
            }
        } else {
            fmt.Printf("%s %s = %v -tag:%s \n", name, t.Field(i).Type, f.Interface(), t.Field(i).Tag)
        }
    }

}

go

posted on 2024-10-18 22:05  dz45693  阅读(8)  评论(0编辑  收藏  举报

导航