golang反射

package main

import (
    "fmt"
    "reflect"
)

func reflect_example(a interface{}) {
    t := reflect.TypeOf(a)
    fmt.Printf("type of a is:%v\n", t)

    k := t.Kind()
    switch k {
    case reflect.Int64:
        fmt.Printf("a is int64\n")
    case reflect.String:
        fmt.Printf("a is string\n")
    }
}

func reflect_value(a interface{}) {
    v := reflect.ValueOf(a)
    // t := reflect.TypeOf(a)
    k := v.Kind()
    //fmt.Printf("a store value is :%d\n", v.Int())
    switch k {
    case reflect.Int64:
        fmt.Printf("a is int64, store value is:%d\n", v.Int())
    case reflect.Float64:
        fmt.Printf("a is float64, store value is:%f\n", v.Float())
    }
}

func reflect_set_value(a interface{}) {
    v := reflect.ValueOf(a)
    // t := reflect.TypeOf(a)
    k := v.Kind()
    //fmt.Printf("a store value is :%d\n", v.Int())
    switch k {
    case reflect.Int64:
        v.SetInt(100)
        fmt.Printf("a is int64, store value is:%d\n", v.Int())
    case reflect.Float64:
        v.SetFloat(6.8)
        fmt.Printf("a is float64, store value is:%f\n", v.Float())
    case reflect.Ptr:
        fmt.Printf("set a to 6.8\n")
        v.Elem().SetFloat(6.8)
    default:
        fmt.Printf("default switch\n")
    }
}

func main() {
    var x float64 = 3.4
    reflect_example(x)
    reflect_value(x)
    reflect_set_value(&x)
    fmt.Printf("x value is %v\n", x)
    /*
        var b *int = new(int)
        *b = 100
    */
}

 

 

package main

import (
    "fmt"
    "reflect"
)

type Student struct {
    Name  string
    Sex   int
    Age   int
    Score float32
    //xxx   int
}

func main() {
    var s Student
    v := reflect.ValueOf(s)
    t := v.Type()
    //t := reflect.TypeOf(s)

    kind := t.Kind()
    switch kind {
    case reflect.Int64:
        fmt.Printf("s is int64\n")
    case reflect.Float32:
        fmt.Printf("s is int64\n")
    case reflect.Struct:
        fmt.Printf("s is struct\n")
        fmt.Printf("field num of s is %d\n", v.NumField())
        for i := 0; i < v.NumField(); i++ {
            field := v.Field(i)
            fmt.Printf("name:%s type:%v value:%v\n",
                t.Field(i).Name, field.Type().Kind(), field.Interface())
        }
    default:
        fmt.Printf("default\n")
    }
}

 

package main

import (
    "fmt"
    "reflect"
)

type Student struct {
    Name  string
    Sex   int
    Age   int
    Score float32
    //xxx   int
}

func main() {
    var s Student
    v := reflect.ValueOf(&s)
    //*v
    v.Elem().Field(0).SetString("stu01")
    v.Elem().FieldByName("Sex").SetInt(2)
    v.Elem().FieldByName("Age").SetInt(18)
    v.Elem().FieldByName("Score").SetFloat(99.2)

    fmt.Printf("s:%#v\n", s)
}

 

package main

import (
    "fmt"
    "reflect"
)

type Student struct {
    Name  string
    Sex   int
    Age   int
    Score float32
    //xxx   int
}

func (s *Student) SetName(name string) {
    s.Name = name
}

func (s *Student) Print() {
    fmt.Printf("通过反射进行调用:%#v\n", s)
}

func main() {
    var s Student
    s.SetName("xxx")
    //SetName(&s, "xxx")
    v := reflect.ValueOf(&s)
    t := v.Type()
    //t := reflect.TypeOf(s)

    fmt.Printf("struct student have %d methods\n", t.NumMethod())
    for i := 0; i < t.NumMethod(); i++ {
        method := t.Method(i)
        fmt.Printf("struct %d method, name:%s type:%v\n", i, method.Name, method.Type)
    }

    //通过reflect.Value获取对应的方法并调用
    m1 := v.MethodByName("Print")
    var args []reflect.Value
    m1.Call(args)

    m2 := v.MethodByName("SetName")
    var args2 []reflect.Value
    name := "stu01"
    nameVal := reflect.ValueOf(name)
    args2 = append(args2, nameVal)
    m2.Call(args2)

    m1.Call(args)
}

 

package main

import (
    "fmt"
    "reflect"
)

type Student struct {
    Name  string `json:"name" db:"name"`
    Sex   int
    Age   int
    Score float32
    //xxx   int
}

func (s *Student) SetName(name string) {
    s.Name = name
}

func (s *Student) Print() {
    fmt.Printf("通过反射进行调用:%#v\n", s)
}

func main() {
    var s Student
    s.SetName("xxx")
    //SetName(&s, "xxx")
    v := reflect.ValueOf(&s)
    t := v.Type()
    //t := reflect.TypeOf(s)

    field0 := t.Elem().Field(0)
    fmt.Printf("tag json=%s\n", field0.Tag.Get("json"))
    fmt.Printf("tag db=%s\n", field0.Tag.Get("db"))

    //json.UnMa
    //var s string

}

 

上一篇:go语言中获取变量类型的三种方法


下一篇:golang-基础