0
点赞
收藏
分享

微信扫一扫

golang反射

醉倾城1 2022-05-13 阅读 100
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
*/
}



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\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")
}
}



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



&{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\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

}



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 \n",user)
user2 := copyPoint(user)
fmt.Printf("%p \n",user)
fmt.Printf("%p \n",user2)
user.Name="lisi"

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

return

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



}


举报

相关推荐

0 条评论