首页  

GO reflect     所属分类 go 浏览量 687
reflect.ValueOf()  reflect.TypeOf()

go doc reflect
func TypeOf(i interface{}) Type
func ValueOf(i interface{}) Value

ValueOf returns a new Value initialized to the concrete value stored in the interface i. 
ValueOf(nil) returns the zero Value.
    
TypeOf returns the reflection Type that represents the dynamic type of i. 
If i is a nil interface value, TypeOf returns nil.
    
go doc reflect.Value

强制转换
realValue := value.Interface().(已知的类型)
强制转换 对类型要求非常严格 ,类型不匹配 直接 panic
转换的时候,要区分是指针还是值


package main

import (
"fmt"
"reflect"
)

func main() {
var num float64 = 1.2345

pointer := reflect.ValueOf(&num)
value := reflect.ValueOf(num)
// 强制转换 对类型要求非常严格 ,类型不匹配 直接 panic
convertPointer := pointer.Interface().(*float64)
convertValue := value.Interface().(float64)

fmt.Println(convertPointer)
fmt.Println(convertValue)
}

0xc000018070
1.2345

上一篇     下一篇
GO make 和 new的区别

GO结构体三种初始化方式

GO 面向对象编程

GO基础选择题

MySQL timestamp 类型

mysql 时间类型