zoukankan      html  css  js  c++  java
  • Go 原理-详解 interface

    golang的interface接口,表示一组方法集合;实现方法的实例可以存储在接口类型的变量;有一种特殊的接口类型就是空接口类型interface{},对于带有方法的接口类型和不带任何方法的 interface{} 类型,底层实现是不一样的。

      空interface{}的底层实现是eface

    type eface struct { // 16 bytes
        _type *_type   //指向底层类型
        data  unsafe.Pointer //指向底层数据(具体值)
    }
    

      带有方法集合的接口底层实现iface

    type iface struct { // 16 bytes
        tab  *itab  //指向下边的结构,出来底层类型外 还包含其他信息
        data unsafe.Pointer //指向底层数据(具体值)
    }
    

      

    type itab struct { // 32 bytes
        inter *interfacetype  //接口类型描述
        _type *_type  //底层类型
        hash  uint32 // copy of _type.hash. Used for type switches. 用于类型转换,转换成具体类型需要判断目标类型和接口的底层类型是否一致
        _     [4]byte
        fun   [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter. //是一个指针数组,每个指针指向具体类型 _type 实现的具体方法
    }
    

      

  • 相关阅读:
    hdu 1312 Red and Black
    hdu 1573 X问题
    广工校赛决赛之简单的数论题
    最大的LeftMax与rightMax之差绝对值
    POJ 2385 Apple Catching
    hdu 1171 Big Event in HDU
    ACM比赛经验
    BestCoder Valentine's Day Round
    使用ffmpeg进行视频封面截取
    使用ODP.NET连接Oracle数据库
  • 原文地址:https://www.cnblogs.com/hellohell/p/13803172.html
Copyright © 2011-2022 走看看