iface 和 eface 都是 Go 中描述接口的底层结构体,区别在于 iface 描述的接口包含方法,而 eface 则是不包含任何方法的空接口:interface{}。
从源码层面看一下:
typeifacestruct{tab*itabdataunsafe.Pointer}typeitabstruct{inter*interfacetype_type*_typelink*itabhashuint32// copy of _type.hash. Used for type switches.badbool// type does not implement interfaceinhashbool// has this itab been added to hash?unused[2]bytefun[1]uintptr// variable sized}
type interfacetype struct {
typ _type
pkgpath name
mhdr []imethod
}
type eface struct {
_type *_type
data unsafe.Pointer
}
package main
import "fmt"
func main() {
x := 200
var any interface{} = x
fmt.Println(any)
g := Gopher{"Go"}
var c coder = g
fmt.Println(c)
}
type coder interface {
code()
debug()
}
type Gopher struct {
language string
}
func (p Gopher) code() {
fmt.Printf("I am coding %s language\n", p.language)
}
func (p Gopher) debug() {
fmt.Printf("I am debuging %s language\n", p.language)
}
go tool compile -S ./src/main.go
func convT2E64(t *_type, elem unsafe.Pointer) (e eface)
func convT2I(tab *itab, elem unsafe.Pointer) (i iface)
type arraytype struct {
typ _type
elem *_type
slice *_type
len uintptr
}
type chantype struct {
typ _type
elem *_type
dir uintptr
}
type slicetype struct {
typ _type
elem *_type
}
type structtype struct {
typ _type
pkgPath name
fields []structfield
}