/
interfaces_cheat_sheet.go
149 lines (128 loc) · 4.02 KB
/
interfaces_cheat_sheet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"fmt"
_ "net/http/pprof"
)
/*
interface: 最简单的interface就是empty interface{}, 所有类型是继承
interface: error也是一个interface, 含Error方法
interface: is a collection of method, and only method
interface: can be implement
interface: only provide method name, argument, return type, method defined is declare in type
interface: type is nil, value is nil when defined, only have type and value when implement
interface: the value and type is dynamic , is the type and value of implement type
interface: a struct can implement multi interface
*/
// Declaring interface type
type RectInterface interface {
area() float64
abc() float64
}
type NameInterface interface {
getName() string
}
// Declaring struct type
type Rect struct {
width float64
height float64
name string
}
type Circle struct {
radius float64
}
// Declaring Rect struct method
func (r *Rect) area() float64 {
return r.width * r.height
}
func (r *Rect) abc() float64 {
return r.width * r.height
}
func (r *Rect) getName() string {
return r.name
}
// Declaring Circle struct method
func (c *Circle) area() float64 {
return c.radius * c.radius
}
func (c *Circle) abc() float64 {
return c.radius * c.radius
}
// --------------------
// Polymorphic function
// --------------------
func GetArea(r RectInterface) float64 {
fmt.Printf("Polymorphic function, Type: %T, area: %v \n", r, r.area())
return r.area()
}
// 充值档位
type ChargeInfo struct {
Id int `json:"id"`
Price int `json:"price"` // 档位金额,单位分
DiscountPrice int `json:"discount_price"` // 支付金额,单位分
Duration int `json:"duration"` // 产生时间,单位秒
ExtraDuration int `json:"extra_duration"` // 额外赠送时间,单位秒
Status int `json:"status"` // 0:正常; 1:禁用
Ctime int64 `json:"ctime,omitempty"`
Mtime int64 `json:"mtime,omitempty"`
}
func test() {
info := &ChargeInfo{
Id: 1,
Price: 1,
DiscountPrice: 1,
Duration: 1,
ExtraDuration: 1,
Status: 1,
Ctime: 1,
Mtime: 1,
}
i := []interface{}{info}
fmt.Println(i[0])
}
type FooInterface interface {
bar() // method receiver is value then interface is value , method receiver is pointer then interface is pointer
}
//Won't throw a compile error. Because the bar method belongs to Foo struct.
type Foo1 struct {
}
func (f Foo1) bar() {
panic("implement me")
}
//Won't throw a compile error. Because the bar method belongs to Foo struct.
//Will throw a error. Because the bar method is bind to &Foo. And when compile checks whether Foo confirms to Foo interface, it will only check the required method existence.
type Foo2 struct {
}
func (f *Foo2) bar() {
panic("implement me")
}
func funcName(foo FooInterface) {
fmt.Println(foo)
}
func main() {
test()
r := &Rect{width: 3, height: 3, name: "betty"}
fmt.Printf("this is Rect, Type: %T, area: %v, abc: %v \n, getName: %v \n", r, r.area(), r.abc(), r.getName())
GetArea(r)
var ri RectInterface
ri = &Rect{width: 3, height: 3}
GetArea(ri)
fmt.Printf("this is RectInterface, Rect Struct, Type: %T, area: %v, abc: %v , no getName \n", ri, ri.area(), ri.abc())
ri = &Circle{radius: 3}
GetArea(ri)
fmt.Printf("this is RectInterface, Circle struct, Type: %T, area: %v, abc: %v , no getName \n", ri, ri.area(), ri.abc())
var ni NameInterface
ni = &Rect{width: 3, height: 3, name: "betty"}
fmt.Printf("this is NameInterface, Rect Struct, Type: %T, getName: %v , no area, no abc \n", ni, ni.getName())
// --------------
// Interface Type assertions
// --------------
var i interface{} = "hello"
fmt.Printf("i am string: %T, %v \n", i, i)
is, ok := i.(string)
fmt.Printf("i am string after type assertions: %T, %v, %v\n", is, is, ok)
i = 1
it, _ := i.(int)
fmt.Printf("i am int after type assertions:%T, %v \n", it, it)
funcName(Foo1{}) // Foo1.bar receiver is value , then Foo1 need value
funcName(&Foo2{}) // Foo2.bar receiver is pointer, then Foo2 need pointer
}