golang的interface

栏目: Go · 发布时间: 5年前

内容简介:1.golang的interface设计golang的interface设计,从是否有方法的角度,分为iface和eface。然后所有的interface内又包含type和value,其中type包含都是结构体rtype,且都继承自接口Type。不同的是,对与纯值interface,eface来说,type就是rtype类型,对于有方法的interface,iface来说,type是用itab来表示。

1.golang的interface设计

golang的interface设计,从是否有方法的角度,分为iface和eface。然后所有的interface内又包含type和value,其中type包含都是结构体rtype,且都继承自接口Type。

不同的是,对与纯值interface,eface来说,type就是rtype类型,对于有方法的interface,iface来说,type是用itab来表示。

itab是运行时虚表,目的是支持golang的接口自动继承特性,其中的interfacetype为包含imethod的虚表,类型断言时比较的就是itab。

2.golang的interface底层实现(1.10.3)。

先看接口的底层实现,,在runtime中还有一份实现,暂时还不明白空接口和接口之间如何完成转换。

type iface struct {
   tab  *itab
   data unsafe.Pointer
}

type eface struct {
   _type *_type
   data  unsafe.Pointer
}
 
// layout of Itab known to compilers
// allocated in non-garbage-collected memory
// Needs to be in sync with
// ../cmd/compile/internal/gc/reflect.go:/^func.dumptypestructs.
type itab struct {
   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 interfacetype struct {
   typ     _type
   pkgpath name
   mhdr    []imethod
}
 
// Needs to be in sync with ../cmd/link/internal/ld/decodesym.go:/^func.commonsize,
// ../cmd/compile/internal/gc/reflect.go:/^func.dcommontype and
// ../reflect/type.go:/^type.rtype.
type _type struct {
   size       uintptr
   ptrdata    uintptr // size of memory prefix holding all pointers
   hash       uint32
   tflag      tflag
   align      uint8
   fieldalign uint8
   kind       uint8
   alg        *typeAlg
   // gcdata stores the GC type data for the garbage collector.
   // If the KindGCProg bit is set in kind, gcdata is a GC program.
   // Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
   gcdata    *byte
   str       nameOff
   ptrToThis typeOff
}

代码在reflect包中也有一份抽象的实现。核心有 emptyInterface、 nonEmptyInterface、 rtype这个类。

// emptyInterface is the header for an interface{} value.
type emptyInterface struct {
   typ  *rtype
   word unsafe.Pointer
}
 
// nonEmptyInterface is the header for an interface value with methods.
type nonEmptyInterface struct {
   // see ../runtime/iface.go:/Itab
   itab *struct {
      ityp *rtype // static interface type
      typ  *rtype // dynamic concrete type
      hash uint32 // copy of typ.hash
      _    [4]byte
      fun  [100000]unsafe.Pointer // method table
   }
   word unsafe.Pointer
}
 
// rtype is the common implementation of most values.
// It is embedded in other, public struct types, but always
// with a unique tag like `reflect:"array"` or `reflect:"ptr"`
// so that code cannot convert from, say, *arrayType to *ptrType.
//
// rtype must be kept in sync with ../runtime/type.go:/^type._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
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

图解机器学习

图解机器学习

杉山将 / 许永伟 / 人民邮电出版社 / 2015-4 / 49

本书用丰富的图示,从最小二乘法出发,对基于最小二乘法实现的各种机器学习算法进行了详细的介绍。第Ⅰ部分介绍了机器学习领域的概况;第Ⅱ部分和第Ⅲ部分分别介绍了各种有监督的回归算法和分类算法;第Ⅳ部分介绍了各种无监督学习算法;第Ⅴ部分介绍了机器学习领域中的新兴算法。书中大部分算法都有相应的MATLAB程序源代码,可以用来进行简单的测试。 本书适合所有对机器学习有兴趣的初学者阅读。 187张图......一起来看看 《图解机器学习》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具