-
Notifications
You must be signed in to change notification settings - Fork 128
/
lesson2.slide
597 lines (313 loc) · 12.5 KB
/
lesson2.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
Go language fundamentals, the first part
Lesson 2
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 #2
Outline:
- keywords
- operators
- defer statement
- structs (records)
- arrays
- slices
- pointers
- maps
- loops that use range
- user-defined data types
############################################################
* Keywords
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
############################################################
* Operators and characters with special meaning
+ & += &= && == != ( )
- | -= |= || < <= [ ]
* ^ *= ^= <- > >= { }
/ << /= <<= ++ = := , ;
% >> %= >>= -- ! ... . :
&^ &^=
############################################################
* defer statement
- `defer` is a keyword in the Go programming language
- used to "remember" commands that will be called before `return` or exit
- based on LIFO (stack) of remembered commands
- parameters are evaluated when `defer` is declared (ie. in runtime)
- (not when the specified code is called)
- it is possible to change function return value(s) via `defer`
* Basic usage of defer statement
- function `on_finish()` is called before exit from `main()`
.play lesson2/01_defer_basic_usage.go
* Function declaration in defer statement
- usually the function definition is part of `defer` statement
- this function is anonymous - lambda, and usually it is a closure as well
- (we will talk about lambdas and closures in the next lesson)
.play lesson2/02_defer_func.go
* Function declaration in defer statement (cont.)
- parenthesis around lambda are not required
.play lesson2/02B_defer_func.go
* Function declaration with parameters in defer statement
- it is possible to specify arguments passed into function in `defer`
.play lesson2/03_defer_with_parameters.go
* More defer statements in one function
- ten `defer` statements
- LIFO behaviour
- (check in runtime how it works)
.play lesson2/04_more_defers.go
* Defer arguments evaluation
- one function can be used in more `defer` statements
- actual parameters are evaluated in runtime
- (check in runtime)
* Defer arguments evaluation (cont.)
.play lesson2/05_defer_arguments_evaluation.go
* Defer arguments evaluation (more complicated example)
- arrays are a bit tricky
- (call by value vs. call by reference)
- (check in runtime, again)
* Defer arguments evaluation (more complicated example, cont.)
.play lesson2/06_defer_arguments_evaluation.go
* Defer and (many) return statements
- `defer` is called even when more `return` statements are used
* Defer and (many) return statements
.play lesson2/07_defer_on_all_returns.go /^package main/,/^func main/
* Defer and (many) return statements (cont.)
.play lesson2/07_defer_on_all_returns.go /^func main/,/^}/
* Practical usage of defer
- how to use `defer` in an application to copy files
.play lesson2/08_defer_practical_usage.go /^package main/,/func copyFile/
* Practical usage of defer (cont.)
.play lesson2/08_defer_practical_usage.go /^func copyFile/,/^}/
* Practical usage of defer (cont.)
.play lesson2/08_defer_practical_usage.go /^func main/,/^}/
* Practical usage of defer
- previous example refactored
.play lesson2/09_defer_practical_usage.go /^package main/,/func copyFile/
* Practical usage of defer (cont.)
.play lesson2/09_defer_practical_usage.go /^func copyFile/,/^}/
* Practical usage of defer (cont.)
.play lesson2/09_defer_practical_usage.go /^func main/,/^}/
* Defer and return values
- it is possible to change function return value(s) via `defer`
- (works because function declared in defer is closure)
- function return values needs to be named!
- used in applications to set `err` return value
* Defer and return values
.play lesson2/10_defer_return_values.go
* Defer and existing app
.play lesson2/on_finish.go
############################################################
* Structs (records)
- a.k.a. records
- user-defined data type (so called named structure)
- or anonymous structure
- dot operator to access struct members
- initialization using {}
- or by using named members (which is more explicit and better)
- structs are comparable
- it is possible to print whole struct
- pass to functions as value or via pointer (by reference)
* Structs and dot operator
.play lesson2/11_struct.go
* Initialization of struct members
- similar to C language initialization
- usage of {} parenthesis
- C-like initialization: order matters!
* Initialization of struct members (cont.)
.play lesson2/12_struct_init.go
* Better (more explicit) initialization of struct members
- struct members are explicitly specified by name
- better readability
- preferred approach
* Better (more explicit) initialization of struct members (cont.)
.play lesson2/13_better_struct_init.go
* Comparison of whole structs is possible!
- `==` and `!=` operators
.play lesson2/14_struct_comparison.go /^func main/,/^}/
* Comparison of whole structs is possible! (cont.)
.play lesson2/14_struct_comparison.go /^func main/,/^}/
* Passing structs as values into functions
- by value
- by reference (via pointer)
.play lesson2/15_print_user.go /package main/,/^func main//
* Passing structs as values into functions (cont.)
.play lesson2/15_print_user.go /^func main/,/^}/
* Struct embedding
.play lesson2/40_struct_embedding.go
* Anonymous structure
- just declare new variable and specify its type to `struct` _something_
var employee struct {
firstName, lastName string
age int
}
############################################################
* Arrays
- basic data type in the Go programming language
- all array items has the same type
- (well, you can use `interface{}` to allow _dynamic_typing_behaviour_)
- type of array is derived from type of items *and* array size
- (unlike slices)
- index in range 0..length-1
- items indexing via [ ] (as in most other languages)
* Basic operations with arrays
.play lesson2/16_arrays.go
* Matrix (two dimensional array)
.play lesson2/16B_arrays.go
* Array copy
- unlike slices, arrays can be copied
.play lesson2/17_array_copy.go
############################################################
* Slices
- proper data type in the Go programming language
- interface to sequences (better than arrays)
- slices are used more often than _plain_ _old_ _arrays_
- type of slice is derived from type of items
- slices has defined length and capacity (those numbers can be different)
- internally a slice is triple: (pointer to the first element + `len` + `cap`)
- so called "slice operator" `[from:to]`
- `append` function to add a new element to slice (complicated internally)
* Usage of slices
.play lesson2/18_slices.go /package main/,/cont//
* Usage of slices (cont.)
.play lesson2/18_slices.go /cont/,/^}//
* Slices and arrays as data source for them
- slice can be created from any array
- but the slice does not contain any data, just a pointer to array element
- so any modify in slice element is reflected in an array as well
.play lesson2/19_slice_copy.go /package main/,/cont//
* Slices and arrays as data source for them (cont.)
- modify array elements
- then modify the same elements, but via slice
.play lesson2/19_slice_copy.go /cont/,/^}//
* Slice from slice
- "slicing" of another slice is possible
.play lesson2/20_slice_from_slice.go /package main/,/cont//
* Slice from slice (cont.)
.play lesson2/20_slice_from_slice.go /cont/,/^}//
* Append function
- the function `append` returns a new slice
- the capacity of new slice can be increased (realloc magic inside!)
.play lesson2/21_slice_append.go
############################################################
* Pointers
- always points to an element of some type
- ie. `void` pointers are not supported
- implicit pointer value is `nil`
- address of element can be retrieved using the `&` operator
- access via pointer (reference in some other languages) using the `*` operator
* Basic usage of pointers
- please note the usage of `*p_i++`
.play lesson2/22_pointer_to_int.go
* Pointer to structure
.play lesson2/23_pointer_to_struct.go /package main/,/func main//
* Pointer to structure (cont.)
- please note the possibility to write `p_u.id` instead of `(*p_u).id`
.play lesson2/23_pointer_to_struct.go /func main/,/^}//
* Pointer to structure item/member
.play lesson2/24_pointer_to_struct_item.go /package main/,/func main//
* Pointer to structure item/member (cont.)
.play lesson2/24_pointer_to_struct_item.go /func main/,/^}//
* Pointer to array item
.play lesson2/25_pointer_to_array.go /package main/,/cont//
* Pointer to array item (cont.)
.play lesson2/25_pointer_to_array.go /cont/,/^}//
* Return pointer to local variable from C function
.code lesson2/25_B_return_pointer.c
* Return pointer to local variable from Go function
.play lesson2/25_B_return_pointer.go
############################################################
* Maps
- a.k.a. associative array or hash
- container for key-value pairs
- "nil map":
var m1 map[string]int
- empty map:
var m3 map[string]int = make(map[string]int)
m3 := make(map[string]int)
* Maps
- four basic operations: add/put, get, delete, and contains
- add/put items to a map:
m3["zero"] = 0
m3["one"] = 1
m3["two"] = 2
- get item from a map:
m3["two"]
- delete from a map
delete(m3, "zero")
- contains
v, exists = m3["two"]
* Uninitialized map ("nil map")
.play lesson2/26_uninitialized_map.go
* Uninitialized map ("nil map")
.play lesson2/26_B_uninitialized_map.go
* Empty map (int -> string)
.play lesson2/27_initialized_map.go
* Empty map (string -> int)
.play lesson2/27_B_initialized_map.go
* Empty map (shorter idiom)
.play lesson2/28_initialized_map.go
* Maps and struct
.play lesson2/29_map_and_struct.go /^package main/,/^func main/
* Maps and struct (cont.)
.play lesson2/29_map_and_struct.go /^func main/,/^}/
* Struct can be used as key as well
.play lesson2/30_map_and_struct_B.go /^package main/,/^func main/
* Struct can be used as key as well (cont.)
.play lesson2/30_map_and_struct_B.go /^func main/,/^}/
* Reading items from map
.play lesson2/31_reading_from_maps.go /^package main/,/cont/
* Nonexistent values
.play lesson2/31_map_nonexistent_values.go
* Reading items from map (cont.)
.play lesson2/31_reading_from_maps.go /cont/,/^}/
* Deleting items from map
.play lesson2/32_delete_from_map.go /^package main/,/^func main/
* Deleting items from map (cont.)
.play lesson2/32_delete_from_map.go /^func main/,/^}/
* Sets
- is is possible to simulate them using maps
map[key_type]struct{}
############################################################
* Loops and the range clause
- (see the 1st lesson for a `for` loop examples)
- `range` keyword used in `for` to loop over array items, map pairs etc.
- provides an item index as well (Java on the other side...)
* Iterating over an array
.play lesson2/33_for_range_1.go
* Index can be ignored
.play lesson2/34_for_range_2.go
* Iterating over slice
.play lesson2/35_for_range_3.go
* Iterating over Unicode glyphs
.play lesson2/36_for_range_4.go
* Iterating over map pairs (key+value)
.play lesson2/37_for_range_map.go
############################################################
* User-defined data types
- `type` keyword
- Go is very strict in typing (see examples)
* User-defined data types
.play lesson2/38_user_types.go
* User-defined data types (passing to function)
.play lesson2/39_type_func_params.go
############################################################
#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