8.1 通过反射获取一个变量的值和类型
package main
import (
"fmt"
"reflect"
)
func main(){
var a float32 = 43.1
t := reflect.TypeOf(a)
v := reflect.ValueOf(a)
fmt.Println("type1 = ", t)
fmt.Println("value1 = ", v)
if t.Name() == "float32" {
fmt.Println("haha1")
}
}
结果:
type1 = float32
value1 = 43.1
haha1
Process finished with the exit code 0