-
Notifications
You must be signed in to change notification settings - Fork 127
/
go_primer.slide
348 lines (247 loc) · 8.55 KB
/
go_primer.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
Go language primer
Red Hat
1 May 2024
Tags: golang, go
Pavel Tišnovský <[email protected]>
Red Hat, Inc.
* 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
* 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
* Packages and Imports
- Program building blocks ("libraries")
- Imported by other programs
- Package name is the last element of the import path
- Standard packages: [[https://golang.org/pkg/]]
.code lesson1/packages.go
* Hello world in Go
.play lesson1/hello3.go
.play lesson1/hello.go
.play lesson1/hello2.go
* Syntax
- Evolved from C
- Declarations in postfix
- Exported symbols begin with Capital letter
- [[https://blog.golang.org/gos-declaration-syntax]]
.code lesson1/syntax_c.go
.code lesson1/syntax_go.go
* Functions
.play lesson1/functions.go
* Multiple return values
.play lesson1/functions2.go
* Named return values
.play lesson1/functions3.go
* Variables
.play lesson1/vars.go
* Types
- Similar to C
- No auto casts
- Type inference && default types
- Each type has clear zero value (0, false, "")
.code lesson1/types.go
* Type conversions
- No auto casts
.play lesson1/casts.go
* Constants
.play lesson1/const.go
* Conditions
- Similarly to for loop allows init statement
- Variables declared there don't exist outside of the if block
- No parenthesis around the condition expression
- Blocks required! (re Apple Goto fail)
.play lesson1/if.go
* Else statement
- Variables declared in the init statement do exist in the else block
- Else on the same line as closing bracket
.play lesson1/if2.go
* Switch statement
- Again allows init statement
- Like set of if-else statements
- Evaluated top-bottom, first successful case is executed
- Thus there's no break statement
.play lesson1/switch.go
* Switch statement 2
- Allows non-constant and non-integer values
.play lesson1/switch2.go
* Switch with no condition
- switch without condition is like switch true
.play lesson1/switch3.go
* Loops
- Just for loop which also serves as while loop and endless loop
.play lesson1/for.go
* Error handling
- error is built-in type
[[https://blog.golang.org/error-handling-and-go]]
.play lesson1/ret.go
* Ignoring return values
- '_' idiom can be used to drop return values
.play lesson1/ret2.go
* 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
* 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
- pass to functions as value or via pointer (by reference)
* Structs and dot operator
.play lesson2/11_struct.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/,/^}/
* 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)
* 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//
* 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
* Maps
- a.k.a. associative array or hash
- container for key-value pairs
- "nil map":
var m1 map[string]int
- empty map:
var m2 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"]
* Loops and the range clause
- `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
* 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
* 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
* 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
* Communication via message queues
- Analogy of phone calls
- Phone ringing: blocking call
- Answerphone: non-blocking call (tape length = queue size)
- Multiple receivers: Call centre
- Multiple senders: Conf call
* Threads and goroutines
- Reminder: Threads share memory
- Go routines are light-weighted threads, cheap to spawn
- It should be fine to spawn thousands of goroutines
- Internally multiplexed across thread pool
- Spawned with simple go keyword
* Goroutines
.play lesson4/goroutines.go
* Channels
- Way to transfer data between goroutines
- Type of data is part of the channel type
- By default blocks on both tx/rx (but can be buffered too)
- Same operator to send and receive from channel
- Created with make()
.code lesson4/chan.go
* Channels
- Note that this is racy.. more about that later
.play lesson4/chan2.go
* Channels
- Unbuffered channels do block (and golang kindly detects deadlock)
.play lesson4/chan3.go