前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang的interface

Golang的interface

作者头像
LinkinStar
发布2022-09-01 13:48:06
2910
发布2022-09-01 13:48:06
举报
文章被收录于专栏:LinkinStar's BlogLinkinStar's Blog

由于golang中说interface的文章太多了,很多都已经说的很细节了,所以我再说感觉也有点难。于是总结出几个关键问题,供你参考,如果能做到准确无误有理有据的回答,那么interface应该是没有问题了。

问题

  1. interface底层结构有哪两种,分别是什么样子的,里面保存了哪些信息?
  2. 其中tab是什么时候生成的?
  3. 从别的类型转换成interface,从interface转换成别的类型,这两者的过程是怎么样的?
  4. 两个interface之间是否可以比较?
  5. golang底层是如何判断一个类型是否实现了一个interface?

1、底层结构

代码语言:javascript
复制
type eface struct { // 16 bytes on a 64bit arch
    _type *_type
    data  unsafe.Pointer
}
type iface struct { // 16 bytes on a 64bit arch
    tab  *itab
    data unsafe.Pointer
}
type itab struct { // 40 bytes on a 64bit arch
    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 _type struct { // 48 bytes on a 64bit arch
    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
}

https://draveness.me/golang/basic/golang-interface.html

2、tab

tab结构是itab,里面包含了interfacetype,_type,fun,编译期生成。 https://github.com/teh-cmc/go-internals/blob/master/chapter2_interfaces/README.md

3、类型转换

由其他类型转换成interface

转eface

转空接口,很简单,将Eface中的data指针指向原型数据,type指针会指向数据的Type结构体。

转iface

与eface相同,但是需要赋值到itab,并且需要做检测,只有实现接口所有方法才可以进行转换。

interface转其他类型

那没话说,直接反射走起 有这样的语法解决v, ok := i.(T) 转换的时候也需要比较能否进行转换

4、类型比较

两个interface是可以比较的, http://docs.studygolang.com/ref/spec#Comparison_operators 其中说到 Interface values are comparable. Two interface values are equal if they have identical dynamic types and equal dynamic values or if both have value nil. 只要两个interface的动态类型相同和值相同就可以。

5、判断实现

这个判断其实在检测的时候都需要用到。 检测就是看Type中的方法表是否包含了InterfaceType的方法表中的所有方法,并把Type方法表中的实现部分拷到Itab的func那张表中。其中表中的数据都是排序过的,所以对比起来快。 https://tiancaiamao.gitbooks.io/go-internals/content/zh/07.2.html

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题
  • 1、底层结构
  • 2、tab
  • 3、类型转换
    • 由其他类型转换成interface
      • 转eface
      • 转iface
    • interface转其他类型
    • 4、类型比较
    • 5、判断实现
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档