美文网首页
Go语言爱好者周刊笔记-接口与反射关系

Go语言爱好者周刊笔记-接口与反射关系

作者: 路人爱早茶 | 来源:发表于2020-04-20 19:41 被阅读0次

    [图片上传中...(image-961710-1587039097333)]

    接口是抽象基本工具

    接口在分配值时候存储类型信息,反射在运行时检查类型和值方法

    像一个接接口分配一个值

    一个接口编码三件事 :值,方法集,以及所存储值类型


    image

    接口内部结构三个部分:_type类型信息,*data指向实际值指针,itab是方法集.
    方法接受接口作为参数,值传递给函数则会将该值,值方法集,类型打包到接口中

    通过反射包运行时检查接口

    一个值存储进接口,reflect检查接口各部分.不能直接检查接口结构而是反射包维护有权访问的接口结构副本

    reflect.Type和reflect.Vallue

    .type 侧重于公开类型相关数据因此只限于_type部分,
    .value则必须将类型信息与值结合起来以检查和操作值.因此必须查看_type和*data部分

    .type检查类型

    .typeof()用于从一个值中提取值类型信息,该函数唯一的参数是一个空接口interface(),传递的值付到空接口,因此该值类型方法集合都可以轻松获得.
    .typeof返回.type类型值有类型.type.size();.type.kind();.type.name();.type.filed()
    .type.numfiled()得到结构中参数数目,.type.field(n).type.kind();

    .method检查itab方法集(结构体所有实现方法)

    .type.method(0);.type.methodbyname("name");.type.Nummethod()

    t.nummethod()

    .value()展示实际存储在接口中的信息

    相关方法必须将类型信息和实际值结合在一起,为了从一个结构中提取字段信息,refelect包必须将结构布局只是,存储在_type中字段和字段偏移量信息与接口*data部分所指实际值结合起来解码

    .value.kind .vallue.field()+.value.pointer()结合找到运行时状态

     1package main
     2
     3import (
     4    "log"
     5    "reflect"
     6)
     7
     8type Child struct {
     9    Name  string
    10    Grade int
    11    Nice  bool
    12}
    13
    14type Adult struct {
    15    Name       string
    16    Occupation string
    17    Nice       bool
    18}
    19
    20// search a slice of structs for Name field that is "Hank" and set its Nice
    21// field to true.
    22func nice(i interface{}) {
    23    // retrieve the underlying value of i.  we know that i is an
    24    // interface.
    25    v := reflect.ValueOf(i)
    26
    27    // we're only interested in slices to let's check what kind of value v is. if
    28    // it isn't a slice, return immediately.
    29    if v.Kind() != reflect.Slice {
    30        return
    31    }
    32
    33    // v is a slice.  now let's ensure that it is a slice of structs.  if not,
    34    // return immediately.
    35    if e := v.Type().Elem(); e.Kind() != reflect.Struct {
    36        return
    37    }
    38
    39    // determine if our struct has a Name field of type string and a Nice field
    40    // of type bool
    41    st := v.Type().Elem()
    42
    43    if nameField, found := st.FieldByName("Name"); found == false || nameField.Type.Kind() != reflect.String {
    44        return
    45    }
    46
    47    if niceField, found := st.FieldByName("Nice"); found == false || niceField.Type.Kind() != reflect.Bool {
    48        return
    49    }
    50
    51    // Set any Nice fields to true where the Name is "Hank"
    52    for i := 0; i < v.Len(); i++ {
    53        e := v.Index(i)
    54        name := e.FieldByName("Name")
    55        nice := e.FieldByName("Nice")
    56
    57        if name.String() == "Hank" {
    58            nice.SetBool(true)
    59        }
    60    }
    61}
    62
    63func main() {
    64    children := []Child{
    65        {Name: "Sue", Grade: 1, Nice: true},
    66        {Name: "Ava", Grade: 3, Nice: true},
    67        {Name: "Hank", Grade: 6, Nice: false},
    68        {Name: "Nancy", Grade: 5, Nice: true},
    69    }
    70
    71    adults := []Adult{
    72        {Name: "Bob", Occupation: "Carpenter", Nice: true},
    73        {Name: "Steve", Occupation: "Clerk", Nice: true},
    74        {Name: "Nikki", Occupation: "Rad Tech", Nice: false},
    75        {Name: "Hank", Occupation: "Go Programmer", Nice: false},
    76    }
    77
    78    log.Printf("adults before nice: %v", adults)
    79    nice(adults)
    80    log.Printf("adults after nice: %v", adults)
    81
    82    log.Printf("children before nice: %v", children)
    83    nice(children)
    84    log.Printf("children after nice: %v", children)
    85}
    

    相关文章

      网友评论

          本文标题:Go语言爱好者周刊笔记-接口与反射关系

          本文链接:https://www.haomeiwen.com/subject/kuhzmhtx.html