Go的interface源碼在Golang源碼的runtime
目錄中。
Go在不同版本之間的interface結構可能會有所不同,但是,整體的結構是不會改變的,此文章用的Go版本是1.11。
Go的interface是由兩種類型來實現的:iface
和eface
。
其中,iface
表示的是包含方法的interface,例如:
type Person interface {
Print()
}
而eface
代表的是不包含方法的interface,即
type Person interface {}
或者
var person interface{} = xxxx實體
eface
eface
的具體結構是:
一共有兩個屬性構成,一個是類型信息_type
,一個是數據信息。
其中,_type
可以認爲是Go語言中所有類型的公共描述,Go語言中幾乎所有的數據結構都可以抽象成_type
,是所有類型的表現,可以說是萬能類型,data
是指向具體數據的指針。
type
的具體