首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go语言核心编程(6)——反射

Go语言核心编程(6)——反射

作者头像
羊羽shine
发布2019-05-29 18:39:40
5620
发布2019-05-29 18:39:40
举报
文章被收录于专栏:Golang开发Golang开发

注:本文是《Go语言核心编程》(李文塔/著)个人读书笔记

reflect.Type
type rtype struct {
    size       uintptr
    ptrdata    uintptr  // number of bytes in the type that can contain pointers
    hash       uint32   // hash of type; avoids computation in hash tables
    tflag      tflag    // extra type information flags
    align      uint8    // alignment of variable with this type
    fieldAlign uint8    // alignment of struct field with this type
    kind       uint8    // enumeration for C
    alg        *typeAlg // algorithm table
    gcdata     *byte    // garbage collection data
    str        nameOff  // string form
    ptrToThis  typeOff  // type for pointer to this type, may be zero
}
26种基础类型
const (
    Invalid Kind = iota
    Bool
    Int
    Int8
    Int16
    Int32
    Int64
    Uint
    Uint8
    Uint16
    Uint32
    Uint64
    Uintptr
    Float32
    Float64
    Complex64
    Complex128
    Array
    Chan
    Func
    Interface
    Map
    Ptr
    Slice
    String
    Struct
    UnsafePointer
)
reflect. Value

reflect. Value 表示实例的值信息, reflect.Value 是一个 struct,并提供了一系列 的 method给使用者 。一个是值的类型指针 typ ,另 一个是指 向值的指针 ptr, 最后 一个是标记宇段 flag。

type Value struct {
    typ *rtype
        ptr unsafe.Pointer
    flag
}
常用API

从实例到 Value 通过实例获取 Value 对象,直接使用 reflect.ValueOf()函数

func ValueOf(i interface {})

从实例到 TypeValue通过实例获取反射对象的 Type,直接使用 reflect.Typeof()函数

func TypeOf(i interface{}) type

从 Type 到Value Type 里面只有类型信息,所以直接从一个Type 接口变量里面是无法获得实例的value的,但可以通过该Type构建一个新实例的Value 。reflect 包提供了两种方法。

func New(typ Type)Value
func Zero(typ Type) Value

如果知道一个类型值的底层存放地址,则还有一个函数是可以依据 type 和该地址值恢复出 Value 的

func NewAt(typ Type,p unsafe . Pointer ) Value

从 Value 到 Type 从反射对象 Value 到 Type 可以直接调用 Value 的方法,因为 Value 内部存放着到 Type 类型 的指针。

func(v Value)Type()Type
反射三定律

1反射可以从接口值得到反射对象 。 2反射可以从反射对象获得接口值。 3若要修改一个反射对象,则其值必须可以修改 。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.04.19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • reflect.Type
    • 26种基础类型
    • reflect. Value
    • 常用API
    • 反射三定律
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档