图解Go语言interface底层实现

Go的interface源码在Golang源码的runtime目录中。
Go在不同版本之间的interface结构可能会有所不同,但是,整体的结构是不会改变的,此文章用的Go版本是1.11。

Go的interface是由两种类型来实现的:ifaceeface
其中,iface表示的是包含方法的interface,例如:

type Person interface {
	Print()
}

eface代表的是不包含方法的interface,即

type Person interface {}

或者

var person interface{} = xxxx实体

eface

eface的具体结构是:
图解Go语言interface底层实现_第1张图片
一共有两个属性构成,一个是类型信息_type,一个是数据信息。
其中,_type可以认为是Go语言中所有类型的公共描述,Go语言中几乎所有的数据结构都可以抽象成_type,是所有类型的表现,可以说是万能类型,
data是指向具体数据的指针。

type的具体

你可能感兴趣的:(图解golang的那些事儿)