golang的interface

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

内容简介: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
}

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

查看所有标签

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

遗传算法与工程优化

遗传算法与工程优化

程润伟 / 清华大学出版社 / 2004-1 / 39.00元

《遗传算法与工程优化》总结了遗传算法在工业工程相关领域应用的前沿进展。全书共分9章:遗传算法基础、组合优化问题、多目标优化问题、模糊优化问题、可靠性设计问题、调度问题、高级运输问题、网络设计与路径问题和制造元设计问题。内容既涵盖了遗传算法在传统优化问题中的新进展,又涉及了目前在供应链和物流研究中相当热门的话题。一起来看看 《遗传算法与工程优化》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

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

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具