golang中的接口分為帶方法的接口和空接口。
帶方法的接口在底層用iface表示,空接口的底層則是eface表示。下面我們透過底層分別看一下這兩種類型的接口原理。
以下是接口的原型:
//runtime/runtime2.go
//非空接口
type iface struct {
tab *itab
data unsafe.Pointer
}
type itab struct {
inter *interfacetype
_type *_type
link *itab
hash uint32 // copy of _type.hash. Used for type switches.
bad bool // type does not implement interface
inhash bool // has this itab been added to hash?
unused [2]byte
fun [1]uintptr // variable sized
}
//******************************
//空接口
type eface struct {
_type *_type
data unsafe.Pointer
}
//========================
//這兩個接口共同的字段_type
//========================
//runtime/type.go
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
}
//_type這個結構體是golang定義數(shù)據(jù)類型要用的,講到反射文章的時候在具體講解這個_type。
1.iface
1.1 變量類型是如何轉(zhuǎn)換成接口類型的?
看下方代碼:
package main
type Person interface {
run()
}
type xitehip struct {
age uint8
}
func (o xitehip)run() {
}
func main() {
var xh Person = xitehip{age:18}
xh.run()
}
xh變量是Person接口類型,那xitehip的struct類型是如何轉(zhuǎn)換成接口類型的呢?
看一下生成的匯編代碼:
0x001d 00029 (main.go:13) PCDATA $2, $0
0x001d 00029 (main.go:13) PCDATA $0, $0
0x001d 00029 (main.go:13) MOVB $0, ""..autotmp_1+39(SP)
0x0022 00034 (main.go:13) MOVB $18, ""..autotmp_1+39(SP)
0x0027 00039 (main.go:13) PCDATA $2, $1
0x0027 00039 (main.go:13) LEAQ go.itab."".xitehip,"".Person(SB), AX
0x002e 00046 (main.go:13) PCDATA $2, $0
0x002e 00046 (main.go:13) MOVQ AX, (SP)
0x0032 00050 (main.go:13) PCDATA $2, $1
0x0032 00050 (main.go:13) LEAQ ""..autotmp_1+39(SP), AX
0x0037 00055 (main.go:13) PCDATA $2, $0
0x0037 00055 (main.go:13) MOVQ AX, 8(SP)
0x003c 00060 (main.go:13) CALL runtime.convT2Inoptr(SB)
0x0041 00065 (main.go:13) MOVQ 16(SP), AX
0x0046 00070 (main.go:13) PCDATA $2, $2
0x0046 00070 (main.go:13) MOVQ 24(SP), CX
從匯編發(fā)現(xiàn)有個轉(zhuǎn)換函數(shù):
runtime.convT2Inoptr(SB)
我們?nèi)タ匆幌逻@個函數(shù)的實現(xiàn):
func convT2Inoptr(tab *itab, elem unsafe.Pointer) (i iface) {
t := tab._type
if raceenabled {
raceReadObjectPC(t, elem, getcallerpc(), funcPC(convT2Inoptr))
}
if msanenabled {
msanread(elem, t.size)
}
x := mallocgc(t.size, t, false)//為elem申請內(nèi)存
memmove(x, elem, t.size)//將elem所指向的數(shù)據(jù)賦值到新的內(nèi)存中
i.tab = tab //設置iface的tab
i.data = x //設置iface的data
return
}
從以上實現(xiàn)我們發(fā)現(xiàn)編譯器生成的struct原始數(shù)據(jù)會復制一份,然后將新的數(shù)據(jù)地址賦值給iface.data從而生成了完整的iface,這樣如下原始代碼中的xh就轉(zhuǎn)換成了Person接口類型。
var xh Person = xitehip{age:18}
用gdb實際運行看一下(見圖1):

convT2Inoptr函數(shù)傳進來的參數(shù)是*itab和源碼中的 *xitehip。
圖2是itab的類型原型和內(nèi)存中的數(shù)據(jù)發(fā)現(xiàn)itab確實是runtime中源碼里的字段。總共占了32個字節(jié)。([4]uint8 不占字節(jié))

圖3是elem的數(shù)據(jù)他是個名為xitehip的結構體類型里面存放的是age=18。
內(nèi)存中的0x12正好是age=18。注意此時的地址是:0xc000032777。

圖4是xh變量的數(shù)據(jù)類型和其中data字段的數(shù)據(jù)。發(fā)現(xiàn)xh確實是iface類型了且xh.data的地址不是上面提到的0xc000032777 而是0xc000014098,證明是復制了一份xitehip類型的struct。

1.2 指針變量類型是如何轉(zhuǎn)換成接口類型的呢?
還是上面的例子只是將
var xh Person = xitehip{age:18}
換成了
var xh Person = &xitehip{age:18}
那指針類型的變量是如何轉(zhuǎn)換成接口類型的呢?
見下方匯編代碼:
0x001d 00029 (main.go:13) PCDATA $2, $1
0x001d 00029 (main.go:13) PCDATA $0, $0
0x001d 00029 (main.go:13) LEAQ type."".xitehip(SB), AX
0x0024 00036 (main.go:13) PCDATA $2, $0
0x0024 00036 (main.go:13) MOVQ AX, (SP)
0x0028 00040 (main.go:13) CALL runtime.newobject(SB)
0x002d 00045 (main.go:13) PCDATA $2, $1
0x002d 00045 (main.go:13) MOVQ 8(SP), AX
0x0032 00050 (main.go:13) MOVB $18, (AX)
發(fā)現(xiàn)了這個函數(shù):
runtime.newobject(SB)
去看一下具體實現(xiàn):
// implementation of new builtin
// compiler (both frontend and SSA backend) knows the signature
// of this function
func newobject(typ *_type) unsafe.Pointer {
return mallocgc(typ.size, typ, true)
}
編譯器自動生成了iface并將&xitehip{age:18}創(chuàng)建的對象的地址(通過newobject)賦值給iface.data。就是xitehip這個結構體沒有被復制。
用gdb看一下見圖5:

1.3 那xh是如何找到run方法的呢?我們繼續(xù)看見圖6,相關解釋在圖中已經(jīng)標注:

1.4 接口調(diào)用規(guī)則
把上面的例子添加一個eat()接口方法并實現(xiàn)它(注意這個接口方法的實現(xiàn)的接受者是指針)。
package main
type Person interface {
run()
eat(string)
}
type xitehip struct {
age uint8
}
func (o xitehip)run() { // //接收方o是值
}
func (o *xitehip)eat(food string) { //接收方o是指針
}
func main() {
var xh Person = &xitehip{age:18} //xh是指針
xh.eat("ma la xiao long xia!")
xh.run()
}
這個例子的xh變量的實際類型是個指針,那它是如何調(diào)用非指針方法run的呢?
繼續(xù)gdb跟蹤一下,見圖7:

直接跟蹤xh.tab.fun的內(nèi)存數(shù)據(jù)發(fā)現(xiàn)eat方法確實在0x44f940。上面已經(jīng)說了fun這個數(shù)組大小只為1那run方法應該在eat的后面,但是gdb沒有提示哪個地方是run的起始位置。為了驗證run就在eat的后面,我直接往下debug看eat的入口地址在哪里,見圖8。

run指令的地址是0x44fa60。那我去打印一下這個地址所指向的具體的值是什么,見圖9:

我們在看一下圖7中,為了更清楚我基于圖7再截一次圖,見圖10:

發(fā)現(xiàn)圖9和和圖10的的run方法的指令是一樣的,證明兩個方法的指令確實一起排列的。
總結,指針類型的對象調(diào)用非指針類型的接收方的方法,編譯器自動將接收方轉(zhuǎn)換為指針類型;調(diào)用方通過xh.tab.fun這個數(shù)組找到對應的方法指令列表。
那xh是值類型的接口,而接口實現(xiàn)的方法的接收方是指針類型,那調(diào)用方可以調(diào)用這個指針方法嗎,答案是不僅不能連編譯都編譯不過去,見圖11:

見下表總結:
| 調(diào)用方 | 接收方 | 能否編譯 |
|---|---|---|
| 值 | 值 | true |
| 值 | 指針 | false |
| 指針 | 值 | true |
| 指針 | 指針 | true |
| 指針 | 指針和值 | true |
| 值 | 指針和值 | false |
從上表可以得出如下結論:
調(diào)用方是值時,只要接收方有指針方法那編譯器不允許通過編譯。
2 eface
空接口相對于非空接口沒有了方法列表。
type eface struct {
_type *_type
data unsafe.Pointer
}
第一個屬性由itab換成了_type,這個結構體是golang中的變量類型的基礎,所以空接口可以指定任意變量類型。
2.1 示例:
cpackage main
import "fmt"
type xitehip struct {
}
func main() {
var a interface{} = xitehip{}
var b interface{} = &xitehip{}
fmt.Println(a)
fmt.Println(b)
}
gdb跟一下見圖12:

2.2斷言
判斷變量數(shù)據(jù)類型
s, ok := i.(TypeName)
if ok {
fmt.Println(s)
}
如果沒有ok的話類型不正確的話會引起panic。
也可以用switch形式:
switch v := v.(type) {
case TypeName:
...
}
3 檢查接口
3.1 利用編譯器檢查接口實現(xiàn)
var _ InterfaceName = (*TypeName)(nil)
3.2 nil和nil interface
3.2.1 nil
func main() {
var i interface{}
if i == nil {
println(“The interface is nil.“)
}
}
(gdb) info locals;
i = {_type = 0x0, data = 0x0}
3.2.2 如果接口內(nèi)部data值為nil,但tab不為空時,此時接口為nil interface。
// go:noinline
func main() {
var o *int = nil
var i interface{} = o
if i == nil {
println("Nil")
}
println(i)
}
(gdb) info locals;
i = {_type = 0x21432f8 <type.*+36723>, data = 0x0}
o = 0x0
3.2.3 利用反射檢查
v := reflect.ValueOf(a)
if v.Isvalid() {
println(v.IsNil()) // true, This is nil interface
}
參考
Go interface實現(xiàn)分析--小米云技術
深度解密Go語言之關于 interface 的10個問題
Go Interface 源碼剖析