golang反射

package main

import (
    "fmt"
    "reflect"
)

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

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

func reflect_value(a interface{}) {
    v := reflect.ValueOf(a)
    // t := reflect.TypeOf(a)
    k := v.Kind()
    //fmt.Printf("a store value is :%d
", v.Int())
    switch k {
    case reflect.Int64:
        fmt.Printf("a is int64, store value is:%d
", v.Int())
    case reflect.Float64:
        fmt.Printf("a is float64, store value is:%f
", 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
", v.Int())
    switch k {
    case reflect.Int64:
        v.SetInt(100)
        fmt.Printf("a is int64, store value is:%d
", v.Int())
    case reflect.Float64:
        v.SetFloat(6.8)
        fmt.Printf("a is float64, store value is:%f
", v.Float())
    case reflect.Ptr:
        fmt.Printf("set a to 6.8
")
        v.Elem().SetFloat(6.8)
    default:
        fmt.Printf("default switch
")
    }
}

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

type of a is:float64
a is float64, store value is:3.400000
set a to 6.8
x value is 6.8

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
")
    case reflect.Float32:
        fmt.Printf("s is int64
")
    case reflect.Struct:
        fmt.Printf("s is struct
")
        fmt.Printf("field num of s is %d
", v.NumField())
        for i := 0; i < v.NumField(); i++ {
            field := v.Field(i)
            fmt.Printf("name:%s type:%v value:%v
",
                t.Field(i).Name, field.Type().Kind(), field.Interface())
        }
    default:
        fmt.Printf("default
")
    }
}

s is struct
field num of s is 4
name:Name type:string value:
name:Sex type:int value:0
name:Age type:int value:0
name:Score type:float32 value:0

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
", s)
}

s:main.Student{Name:"stu01", Sex:2, Age:18, Score:99.2}

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
", 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
", t.NumMethod())
    for i := 0; i < t.NumMethod(); i++ {
        method := t.Method(i)
        fmt.Printf("struct %d method, name:%s type:%v
", 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)
}

&{xxx 0 0 0}
struct student have 2 methods
struct 0 method, name:Print type:func(*main.Student)
struct 1 method, name:SetName type:func(*main.Student, string)
通过反射进行调用:&main.Student{Name:"xxx", Sex:0, Age:0, Score:0}
通过反射进行调用:&main.Student{Name:"stu01", Sex:0, Age:0, Score:0}

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
", 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
", field0.Tag.Get("json"))
    fmt.Printf("tag db=%s
", field0.Tag.Get("db"))

    //json.UnMa
    //var s string

}

tag json=name
tag db=name

通过反射 复制一个指针对象

package main

import (
    "fmt"
    "reflect"
)

type User struct {
    Name string
    Age int
}

func copyPoint(m *User) *User{
    vt := reflect.TypeOf(m).Elem()
    fmt.Println(vt)
    newoby := reflect.New(vt)
    newoby.Elem().Set(reflect.ValueOf(m).Elem())
    return newoby.Interface().(*User)
}

func main(){
    user := &User{}
    user.Name  = "sunlong"
    user.Age  = 32
    fmt.Println(user)
    fmt.Printf("%p 
",user)
    user2 := copyPoint(user)
    fmt.Printf("%p 
",user)
    fmt.Printf("%p 
",user2)
    user.Name="lisi"

    fmt.Printf("%v 
",user)
    fmt.Printf("%v 
",user2)

    return

    //user2 := *user
    //fmt.Printf("%v 
",user)
    //fmt.Printf("%v 
",user2)
    //user2.Age = 33
    //user2.Name = "lisi"
    //fmt.Println(*user)
    //fmt.Println(user2)



}
原文地址:https://www.cnblogs.com/sunlong88/p/11704433.html