iface 和 eface 都是 Go 中描述接口的底层结构体,区别在于 iface 描述的接口包含方法,而 eface 则是不包含任何方法的空接口:interface{}。
从源码层面看一下:
golang
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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}
package main
import"fmt"funcmain() {
x :=200varanyinterface{} = 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)
}
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
}
评论