type MyInt int var i int var j MyInt
var r io.Reader
var r io.Reader
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
if err != nil {
return nil, err
}
r = tty
到这里,r的类型是什么?r的类型仍然是interface io.Reader,只是r = tty这一句,隐含了一个类型转换,将tty转成了io.Reader。
type Binary uint64
func (i Binary) String() string {
return strconv.Uitob64(i.Get(), 2)
}
func (i Binary) Get() uint64 {
return uint64(i)
}

var w io.Writer
switch v := any.(type) {
case int:
return strconv.Itoa(v)
case float:
return strconv.Ftoa(v, ‘g‘, -1)
}
1 var any interface{} // initialized elsewhere
2 s := any.(Stringer) // dynamic conversion
3 for i := 0; i < 100; i++ {
4 fmt.Println(s.String())
5 }
143 type iface struct {
144 tab *itab // 指南itable
145 data unsafe.Pointer // 指向真实数据
146 }
617 type itab struct {
618 inter *interfacetype
619 _type *_type
620 link *itab
621 bad int32
622 unused int32
623 fun [1]uintptr // variable sized
624 }
310 type imethod struct {
311 name nameOff
312 ityp typeOff
313 }
314
315 type interfacetype struct {
316 typ _type
317 pkgpath name
318 mhdr []imethod
319 }
320
28 type _type struct {
29 size uintptr
30 ptrdata uintptr // size of memory prefix holding all pointers
31 hash uint32
32 tflag tflag
33 align uint8
34 fieldalign uint8
35 kind uint8
36 alg *typeAlg
37 // gcdata stores the GC type data for the garbage collector.
38 // If the KindGCProg bit is set in kind, gcdata is a GC program.
39 // Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
40 gcdata *byte
41 str nameOff
42 ptrToThis typeOff
43 }
22 func itabhash(inter *interfacetype, typ *_type) uint32 {
23 // compiler has provided some good hash codes for us.
24 h := inter.typ.hash
25 h += 17 * typ.hash
26 // TODO(rsc): h += 23 * x.mhash ?
27 return h % hashSize
28 }
44 h := itabhash(inter, typ)
45
46 // look twice - once without lock, once with.
47 // common case will be no lock contention.
48 var m *itab
49 var locked int
50 for locked = 0; locked < 2; locked++ {
51 if locked != 0 {
52 lock(&ifaceLock)
53 }
54 for m = (*itab)(atomic.Loadp(unsafe.Pointer(&hash[h]))); m != nil; m = m.link {
55 if m.inter == inter && m._type == typ {
71 return m // 找到了前面计算过的itab
72 }
73 }
74 }
75 // 没有找到,生成一个,并加入到itab的hash中。
76 m = (*itab)(persistentalloc(unsafe.Sizeof(itab{})+uintptr(len(inter.mhdr)-1)*sys.PtrSize, 0, &memstats.other_sys))
77 m.inter = inter
78 m._type = typ
79 additab(m, true, canfail)
13 const ( 14 hashSize = 1009 15 ) 16 17 var ( 18 ifaceLock mutex // lock for accessing hash 19 hash [hashSize]*itab 20 )
92 // both inter and typ have method sorted by name,
93 // and interface names are unique,
94 // so can iterate over both in lock step;
95 // the loop is O(ni+nt) not O(ni*nt). // 按name排序过的,因此这里的匹配只需要O(ni+nt)
99 j := 0
100 for k := 0; k < ni; k++ {
101 i := &inter.mhdr[k]
102 itype := inter.typ.typeOff(i.ityp)
103 name := inter.typ.nameOff(i.name)
104 iname := name.name()
109 for ; j < nt; j++ {
110 t := &xmhdr[j]
111 tname := typ.nameOff(t.name)
112 if typ.typeOff(t.mtyp) == itype && tname.name() == iname {
118 if m != nil {
119 ifn := typ.textOff(t.ifn)
120 *(*unsafe.Pointer)(add(unsafe.Pointer(&m.fun[0]), uintptr(k)*sys.PtrSize)) = ifn // 找到匹配,将实际类型的方法填入itab的fun
121 }
122 goto nextimethod
123 }
124 }
125 }
135 nextimethod:
136 }
140 h := itabhash(inter, typ) //插入上面的全局hash
141 m.link = hash[h]
142 atomicstorep(unsafe.Pointer(&hash[h]), unsafe.Pointer(m))
143 }
到这里,interface的数据结构的框架。
原文:http://www.cnblogs.com/qqmomery/p/6298771.html