-
Notifications
You must be signed in to change notification settings - Fork 127
/
lesson3.slide
389 lines (222 loc) · 8.63 KB
/
lesson3.slide
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
Go language fundamentals, second part
Lesson 3
30 May 2024
Tags: golang, go
Pavel Tišnovský <[email protected]>
Red Hat, Inc.
https://github.com/RedHatOfficial/GoCourse
@RedHat
* Sources
- [[https://github.com/RedHatOfficial/GoCourse]]
.image ./common/qr_address.png
* Gophers
#The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
#Source https://golang.org/doc/gopher/fiveyears.jpg
#The design and this image is licensed under the Creative Commons 3.0 Attributions license.
.image ./common/fiveyears.jpg _ 900
############################################################
* Lesson #3
Outline:
- Lambda functions
- Closures
- Methods on types
- Interfaces
- Type assertions
- Overview of standard libraries
- "Line of sight"
############################################################
* Lambda functions
- a.k.a. function literals
- https://golang.org/ref/spec#Function_literals
- can be assigned to a variable or invoked directly
* Typical usages for lambda functions
- go blocks
- defer statement
- callback functions
- 'compare' for sorting
* Function as a value (like any other value)
.play lesson3/01_func_type.go
* Function with parameters as a value (like any other value)
.play lesson3/02_func_type.go
* Functions stored in local variables (just a toy)
.play lesson3/03_functions_as_values.go
* Anonymous functions (just a toy again)
.play lesson3/04_lambdas.go
* Function types
- a new type `BinaryOp` is defined
- it is used to specify parameter type in `applyBinaryOp`
.play lesson3/05_function_type.go /^package main/,/^func testBinaryOps/
* Function types (cont.)
.play lesson3/05_function_type.go /^func testBinaryOps/,/^}/
############################################################
* Closures
- lambda functions that refer to variables defined outside the function
- it "closes over" another function -> the name closure
- code pointer and environment pointer internally
- useful for function that needs to store its state "somewhere"
* Practical example - sorting elements in slice
.play lesson3/06_sort_function.go /^package main/,/^func main/
* Practical example - sorting elements in slice (cont.)
.play lesson3/06_sort_function.go /^func main/,/^}/
* Practical example - sorting elements in a slice
- implemented as a closure
.play lesson3/07_sort_closure.go /^package main/,/^func main/
* Practical example - sorting elements in slice (cont.)
- implemented as a closure
.play lesson3/07_sort_closure.go /^func main/,/^}/
* Implementation of sort-by
.play lesson3/08_sort_by.go /^package main/,/^func main/
* Implementation of sort-by (cont.)
.play lesson3/08_sort_by.go /^func main/,/^}/
############################################################
* Methods on types
- a _method_ is a function that has a defined receiver
- (in OOP: a method is a function on an instance of an object)
- concept of a _receiver_
- usage - method declaration
func (receiver) method_name(parameters) return_types {
...
...
...
}
- method is called by
some_object.method_name(parameters)
- methods can modify (mutate) a receiver
- call by value vs. call by reference (via pointer)
* User type with one method
.play lesson3/09_methods.go
* Receiver used as a value
.play lesson3/10_methods_with_parameters.go
* Receiver used via pointer
.play lesson3/11_methods_ptr.go
* Shorter variant
.play lesson3/11_methods_ptr_2.go
* Nil receiver
.play lesson3/21_nil_receiver.go
* More methods for the same type
.play lesson3/12_more_methods.go /// Line/,/End OMIT/
############################################################
* Interfaces
- set of methods required to implement such interface
- declared via `interface` keyword:
type XI interface {
method1()
method2(int) int
}
- interface type — variable of interface type, can hold any value implementing an interface
- variable of interface type *I* can hold any value _implementing_ *I*
* Implementing an interface
- a.k.a. _satisfying_ an interface
- no such keyword (`implements` or anything similar)
- in Go: every type which implements all interface’s method automatically satisfies such interface
* How to satisfy an interface
- artificial example
.play lesson3/13_B_satisfy.go
* Embedding other interface(s)
- embedding other interfaces is possible
type XII interface {
m2()
XI
}
- order does not matter there
* Circular embedding
- forbidden, detected by compiler
type I1 interface {
I2
method_i1()
}
type I2 interface {
I3
method_i2()
}
type I3 interface {
I1
method_i3()
}
* Declaration of two interfaces
.play lesson3/13_interface.go
* Missing interface implementation
.play lesson3/14_interface_missing_implementation.go /^package main/,/^func main/
* Missing interface implementation (cont.)
.play lesson3/14_interface_missing_implementation.go /^func main/,/^}/
* Implementation of an interface
.play lesson3/15_interface_implementation_via_methods.go /^package main/,/^func main/
* Implementation of an interface (cont.)
.play lesson3/15_interface_implementation_via_methods.go /^func main/,/^}/
* More implementations of one interface (1/5)
- the longest example :)
.play lesson3/16_more_interface_implementations.go /^package main/,/^type Line/
* More implementations of one interface (2/5)
.play lesson3/16_more_interface_implementations.go /^type Line/,/^func/
* More implementations of one interface (3/5)
.play lesson3/16_more_interface_implementations.go /^func \(line/,/^func main/
* More implementations of one interface (4/5)
.play lesson3/16_more_interface_implementations.go /^func main/,/cont/
* More implementations of one interface (5/5)
.play lesson3/16_more_interface_implementations.go /cont/,/^}/
* Empty interface
- is automatically satisfied by any type
- value of any type can be assigned to such interface type variable
.play lesson3/17_empty_interface.go
* Type `any`
- basically
type any interface{}
- used in standard library at many places
* `nil` interface
- Interface type value consists of two components: dynamic type and dynamic value
- *static* *type*: known by compiler, from the declaration
- *dynamic* *type*: type of assigned _value_
- *dynamic* *value*: the actual value assigned
- where's the problem?
- interface type value is `nil` if both dynamic value *and* dynamic type are `nil`
- sometimes we get the impression that `nil`!=`nil`
* `nil` interface
.play lesson3/18_nil_interface_riddle.go
* `nil` interface
.play lesson3/20_three_nil_interfaces.go
############################################################
* Type assertions
- access to an interface value's underlying concrete value
variable := iface.(Type)
- two operations:
- check that the interface value `iface` holds the concrete type `Type` (with panicking)
- assign the underlying `Type` value to the variable `variable`.
variable, ok := iface.(Type)
- check that the interface value `iface` holds the concrete type `Type`
- if check failed, `variable` will contain zero value and `ok` will be `false`
- if check passed, assign the underlying `Type` value to the variable `variable`, `ok` will be `true`
* Type assertions
.play lesson3/19_type_assertions.go
* Type assertions
.play lesson3/handle_server_error.go
* Panic
.play lesson3/panic.go
############################################################
* Go keywords
break case chan const continue
default defer else fallthrough for
func go goto if import
interface map package range return
select struct switch type var
- https://github.com/tisnik/go-root/blob/master/tools/keywords_frequency.txt
############################################################
* Overview of standard libraries
- Go is used for making CLI apps, network tools, web servers etc.
- it is reflected in its standard library
- https://golang.org/pkg/
############################################################
* Line of sight
- About idiomatic and easy to comprehend Go code
- [[https://medium.com/@matryer/line-of-sight-in-code-186dd7cdea88]]
- "Happy path" on the left (one column)
- No nest of indented braces for "happy path"
- Exit early
- No else returns style statements (`golint` is able to detect it)
- "Happy return" at the very last line
############################################################
#last slide
* More Gophers
#The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
#Source https://golang.org/doc/gopher/bumper.png
#The design and this image is licensed under the Creative Commons 3.0 Attributions license.
.image ./common/bumper.png _ 900