exm 1
package main
import (
"fmt"
"reflect"
)
// reflect demo
func reflectType(x interface{}) {
// 我不知道别人调用我这个函数的时候会传进来什么类型的变量
// 1. 方法1:通过类型断言
// 2. 方法2:借助反射
obj := reflect.TypeOf(x)
fmt.Println(obj)
fmt.Printf("%T\n", obj)
}
func main() {
var a float32 = 1.234
reflectType(a)
var b int8 = 10
reflectType(b)
}
运行结果:
float32
*reflect.rtype
int8
*reflect.rtype
exm 2
package main
import (
"fmt"
"reflect"
)
// reflect demo
func reflectType(x interface{}) {
// 我不知道别人调用我这个函数的时候会传进来什么类型的变量
// 1. 方法1:通过类型断言
// 2. 方法2:借助反射
obj := reflect.TypeOf(x)
fmt.Println(obj)
fmt.Printf("%T\n", obj)
}
type Cat struct{}
type Dog struct{}
func main() {
var a float32 = 1.234
reflectType(a)
var b int8 = 10
reflectType(b)
var c Cat
reflectType(c)
var d Dog
reflectType(d)
}
运行结果:
float32
*reflect.rtype
int8
*reflect.rtype
main.Cat
*reflect.rtype
main.Dog
*reflect.rtype
exm 3
package main
import (
"fmt"
"reflect"
)
// reflect demo
func reflectType(x interface{}) {
// 我不知道别人调用我这个函数的时候会传进来什么类型的变量
// 1. 方法1:通过类型断言
// 2. 方法2:借助反射
obj := reflect.TypeOf(x)
fmt.Println(obj, obj.Name(), obj.Kind())
fmt.Printf("%T\n", obj)
}
type Cat struct{}
type Dog struct{}
func main() {
var a float32 = 1.234
reflectType(a)
var b int8 = 10
reflectType(b)
// 结构体类型
var c Cat
reflectType(c)
var d Dog
reflectType(d)
}
运行结果:
float32 float32 float32
*reflect.rtype
int8 int8 int8
*reflect.rtype
main.Cat Cat struct
*reflect.rtype
main.Dog Dog struct
*reflect.rtype
exm 4
package main
import (
"fmt"
"reflect"
)
// reflect demo
func reflectType(x interface{}) {
// 我不知道别人调用我这个函数的时候会传进来什么类型的变量
// 1. 方法1:通过类型断言
// 2. 方法2:借助反射
obj := reflect.TypeOf(x)
fmt.Println(obj, obj.Name(), obj.Kind())
fmt.Printf("%T\n", obj)
}
type Cat struct{}
type Dog struct{}
func main() {
var a float32 = 1.234
reflectType(a)
var b int8 = 10
reflectType(b)
// 结构体类型
var c Cat
reflectType(c)
var d Dog
reflectType(d)
// slice
var e []int
reflectType(e)
var f []string
reflectType(f)
}
运行结果:
float32 float32 float32
*reflect.rtype
int8 int8 int8
*reflect.rtype
main.Cat Cat struct
*reflect.rtype
main.Dog Dog struct
*reflect.rtype
[]int slice
*reflect.rtype
[]string slice
*reflect.rtype
.Name()空
exm 5
package main
import (
"fmt"
"reflect"
)
// reflect demo
func reflectType(x interface{}) {
// 我不知道别人调用我这个函数的时候会传进来什么类型的变量
// 1. 方法1:通过类型断言
// 2. 方法2:借助反射
obj := reflect.TypeOf(x)
fmt.Println(obj, obj.Name(), obj.Kind())
fmt.Printf("%T\n", obj)
}
func reflectValue(x interface{}) {
v := reflect.ValueOf(x)
fmt.Printf("%v,%T\n", v, v)
}
type Cat struct{}
type Dog struct{}
func main() {
//var a float32 = 1.234
//reflectType(a)
//var b int8 = 10
//reflectType(b)
结构体类型
//var c Cat
//reflectType(c)
//var d Dog
//reflectType(d)
slice
//var e []int
//reflectType(e)
//var f []string
//reflectType(f)
// valueOf
var aa int32 = 100
reflectValue(aa)
}
运行结果:
100,reflect.Value
exm 6
package main
import (
"fmt"
"reflect"
)
// reflect demo
func reflectType(x interface{}) {
// 我不知道别人调用我这个函数的时候会传进来什么类型的变量
// 1. 方法1:通过类型断言
// 2. 方法2:借助反射
obj := reflect.TypeOf(x)
fmt.Println(obj, obj.Name(), obj.Kind())
fmt.Printf("%T\n", obj)
}
func reflectValue(x interface{}) {
v := reflect.ValueOf(x)
fmt.Printf("%v,%T\n", v, v)
k := v.Kind() // 拿到值对应的类型种类
fmt.Println(k)
// 如何得到一个传入时候类型的变量
switch k {
case reflect.Float32:
// 把反射到的值转换成一个int32类型的变量
ret := float32(v.Float())
fmt.Printf("%v %T\n", ret, ret)
case reflect.Int32:
ret := int32(v.Int())
fmt.Printf("%v %T\n", ret, ret)
}
}
type Cat struct{}
type Dog struct{}
func main() {
//var a float32 = 1.234
//reflectType(a)
//var b int8 = 10
//reflectType(b)
结构体类型
//var c Cat
//reflectType(c)
//var d Dog
//reflectType(d)
slice
//var e []int
//reflectType(e)
//var f []string
//reflectType(f)
// valueOf
var aa int32 = 100
reflectValue(aa)
var bb float32 = 1.234
reflectValue(bb)
}
运行结果:
100,reflect.Value
int32
100 int32
1.234,reflect.Value
float32
1.234 float32
exm 7
package main
import (
"fmt"
"reflect"
)
// reflect demo
func reflectType(x interface{}) {
// 我不知道别人调用我这个函数的时候会传进来什么类型的变量
// 1. 方法1:通过类型断言
// 2. 方法2:借助反射
obj := reflect.TypeOf(x)
fmt.Println(obj, obj.Name(), obj.Kind())
fmt.Printf("%T\n", obj)
}
func reflectValue(x interface{}) {
v := reflect.ValueOf(x)
fmt.Printf("%v,%T\n", v, v)
k := v.Kind() // 拿到值对应的类型种类
fmt.Println(k)
// 如何得到一个传入时候类型的变量
switch k {
case reflect.Float32:
// 把反射到的值转换成一个int32类型的变量
ret := float32(v.Float())
fmt.Printf("%v %T\n", ret, ret)
case reflect.Int32:
ret := int32(v.Int())
fmt.Printf("%v %T\n", ret, ret)
}
}
func reflectSetValue(x interface{}) {
v := reflect.ValueOf(x)
k := v.Kind()
switch k {
case reflect.Int32:
v.SetInt(100)
case reflect.Float32:
v.SetFloat(3.21)
}
}
type Cat struct{}
type Dog struct{}
func main() {
//var a float32 = 1.234
//reflectType(a)
//var b int8 = 10
//reflectType(b)
结构体类型
//var c Cat
//reflectType(c)
//var d Dog
//reflectType(d)
slice
//var e []int
//reflectType(e)
//var f []string
//reflectType(f)
// valueOf
//var aa int32 = 100
//reflectValue(aa)
//var bb float32 = 1.234
//reflectValue(bb)
// set value
var aaa int32 = 10
reflectSetValue(aaa)
fmt.Println(aaa)
}
发生死锁panic:
panic: reflect: reflect.Value.SetInt using unaddressable value
goroutine 1 [running]:
reflect.flag.mustBeAssignableSlow(0x85)
/usr/local/go/src/reflect/value.go:262 +0xac
reflect.flag.mustBeAssignable(...)
/usr/local/go/src/reflect/value.go:249
reflect.Value.SetInt({0x104e2e1e0, 0x104e96990, 0x85}, 0x64)
/usr/local/go/src/reflect/value.go:1991 +0x34
main.reflectSetValue({0x104e2e1e0, 0x104e96990})
/Users/jiejaitt/GolandProjects/gin_demo/reflect_demo/main.go:41 +0x130
main.main()
/Users/jiejaitt/GolandProjects/gin_demo/reflect_demo/main.go:75 +0x38
exm 6.5
package main
import (
"fmt"
"reflect"
)
// reflect demo
func reflectType(x interface{}) {
// 我不知道别人调用我这个函数的时候会传进来什么类型的变量
// 1. 方法1:通过类型断言
// 2. 方法2:借助反射
obj := reflect.TypeOf(x)
fmt.Println(obj, obj.Name(), obj.Kind())
fmt.Printf("%T\n", obj)
}
func reflectValue(x interface{}) {
v := reflect.ValueOf(x)
fmt.Printf("%v,%T\n", v, v)
k := v.Kind() // 拿到值对应的类型种类
fmt.Println(k)
// 如何得到一个传入时候类型的变量
switch k {
case reflect.Float32:
// 把反射到的值转换成一个int32类型的变量
ret := float32(v.Float())
fmt.Printf("%v %T\n", ret, ret)
case reflect.Int32:
ret := int32(v.Int())
fmt.Printf("%v %T\n", ret, ret)
}
}
func reflectSetValue(x interface{}) {
v := reflect.ValueOf(x)
// Elem()用来根据指针取对应的值
k := v.Elem().Kind()
switch k {
case reflect.Int32:
v.Elem().SetInt(100)
case reflect.Float32:
v.Elem().SetFloat(3.21)
}
}
type Cat struct{}
type Dog struct{}
func main() {
//var a float32 = 1.234
//reflectType(a)
//var b int8 = 10
//reflectType(b)
结构体类型
//var c Cat
//reflectType(c)
//var d Dog
//reflectType(d)
slice
//var e []int
//reflectType(e)
//var f []string
//reflectType(f)
// valueOf
//var aa int32 = 100
//reflectValue(aa)
//var bb float32 = 1.234
//reflectValue(bb)
// set value
var aaa int32 = 10
reflectSetValue(&aaa)
fmt.Println(aaa)
}
运行结果:
100