-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
257 lines (210 loc) · 4.91 KB
/
README
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
Default variable:
var i int // 0
var a, b, c bool // false
i int
Assign value:
// Short define variable with starting value
a := 1
b := a + 1
// Standard style for define variable
var x string
x = "Hello world"
// or
var x string = "Hello World"
var x = "Hello World"
// Define multiple vars
var (
a = 5
b = 10
c = 15
)
// Error: syntax error: unexpected string at end of statement
x string
x = "Hello world"
// Error: undefined: x
x = "Hello World"
** Define function with pararam input and return
func swap(x, y int) int {}
func swap(x, y string) (string, string) {}
func swap(x, y string) (z string) {}
func slip(x, y int) (a, b int) {} // a, b is defined in the top of function, and return nothing.
for
// using as while, for
for i <= 10 {}
for i; i <= 10; i++ {}
for key, value := range anArray {}
// _ means not use _ variable in for scope.
for _, value := range anArray {}
// like while
for true {}
if // the same other languages
switch ... case // without break at end of case.
** Arrays
var x [5]int
x[0] = 1
....
x := [5]int{1,2,3,4,5};
x := [5]int{
1,
2,
3,
4,
5, // the comma is required.
};
Slices // A slice is a segment of an array. Like arrays slices are indexable and have a length. Unlike arrays this length is allowed to change.
var x []int // create a slices with length is 0
// create a slice with length is 2
x := make([]int, 2)
// min length 5, max length 10
x := make([]float64, 5, 10)
x := []int{1,2,3,4,5};
// Get list value of x from 0 to 3
a := x[0:3];
Slice Functions: append, copy
slice1 := []int{1,2,3}
slice2 := append(slice1, 4, 5)
fmt.Println(slice1, slice2) // [1 2 3] [1 2 3 4 5]
slice1 := []int{1,2,3}
slice2 := make([]int, 2)
copy(slice2, slice1)
fmt.Println(slice1, slice2) // [1 2 3] [1 2]
// Error: first argument to append must be slice; have [3]int
slice1 := [3]int{1,2,3}
slice2 := append(slice1, 4, 5) // Error here
Maps: A map is an unordered collection of key-value pairs.
// Not initialize, can not use.
var x map[string]int
x := make(map[string]int)
x["key"] = 10
fmt.Println(x["key"], len(x)) // 10 1
// Get length of map
len(x)
// Delete an item in map
delete(x, "key");
// get element not in map.
fmt.Println(x["unknow"]) // 0
// Check element in map.
if name, ok := x["key"]; ok {
fmt.Println(name, ok)
}
// Short initilize map
elements := map[string]string{
"H": "Hydrogen",
"He": "Helium",
}
// Map in map
elements := map[string]map[string]string{
"H": map[string]string{
"name":"Hydrogen",
"state":"gas",
},
"He": map[string]string{
"name":"Helium",
"state":"gas",
},
}
** Functions
// params type and output type
func sum(a int, b int) int { return a + b}
// return multiple value
func sum(a int, b int) (int, int) { return (a + b), (a - b)}
// get results
x, y := sum(1, 2);
// Variadic Functions: dynamic input
func add(args ...int) int {}
fmt.Println(add(1,2,3))
// Closure
func main() {
add := func(x, y int) int {
return x + y
}
fmt.Println(add(1,1))
}
// return a function.
func makeEvenGenerator() func() uint {
i := uint(0)
return func() (ret uint) {
ret = i
i += 2
return
}
}
nextEven := makeEvenGenerator()
fmt.Println(nextEven()) // 0
fmt.Println(nextEven()) // 2
fmt.Println(nextEven()) // 4
// Recursion
func factorial(x uint) uint {
if x == 0 {
return 1
}
return x * factorial(x-1)
}
Defer, Panic & Recover
// make sure after complete function, do the other function begining with "defer" keyword
f, _ := os.Open(filename)
defer f.Close() // must close file after Open, but when we've done to use file.
** Pointers
func zero(xPtr *int) {
*xPtr = 0
}
zero(&x);
// Or
x := new(int); // create a pointer
zero(x);
** Structs and Interfaces
Structs
type Circle struct {
x float64
y float64
r float64
}
type Circle struct {
x, y, r float64
}
** Initialization
var c Circle
// Or
c := new(Circle)
// Or
c := Circle{x: 0, y: 0, r: 5}
// Or
c := Circle{0, 0, 5}
// Access fields
fmt.Println(c.x, c.y, c.r)
c.x = 10
c.y = 5
** Method
func (c Circle) area() float64 {
return math.Pi * c.r*c.r
}
c.area();
** Embedded type
type Person struct {
Name string
}
func (p *Person) Talk() {
fmt.Println("Hi, my name is", p.Name)
}
type Android struct {
Person // embedded Person into Android. That means Android is Person
Model string
}
a := new(Android)
a.Talk();
** Interfaces
type Shape interface {
area() float64
}
// using as an input
func totalArea(shapes ...Shape) float64 {
var area float64
for _, s := range shapes {
area += s.area()
}
return area
}
// As a field
type MultiShape struct {
shapes []Shape
}