-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgomeetup.slide
485 lines (171 loc) · 6.01 KB
/
gomeetup.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
# Time to Go
Dimitris Dranidis
CITY College, University of York Europe Campus
1st SKG Gophers Meetup
Friday, May 7, 2021
dranidis@york.citycollege.eu
## Slides and code at
.link https://github.com/dranidis/time-to-go
## Hello world
.play -edit code/hello/hello.go
## Hello Web
Starts a web server at <http://localhost:8080> using the default HTTP router.
.play code/helloweb/helloweb.go
## Go history
- created at Google, 2009
- by Robert Griesemer, Rob Pike, and Ken Thompson
- open source
- First release in 2012, Go 1
- May 2021, Go 1.16
## Go features
- statically typed (with implicit type inference)
- compiled (fast compilation)
- gargage collection
- object-oriented (??)
- functional
- concurrent
## Code organization and tools
## Tools
- `go mod init` - Initialize a module
- `go build` - Build files
- `go install` -- Install binaries in pkg and bin
- `go test ./...` Run all tests and report results
- `go fmt` Format code
- `go doc`-Get help on a command
- `go ...`
## Some language features
## Features
Statically typed with implicit type inference at compile time
i := 3 // same with var i int = 3
Multiple assignments
a, b = b, a
Multiple return values
func foo() (int, int) {
...
return a, b
}
x, y := foo()
Package visibility
func Foo() {...} // visible outside the package
func foo() {...} // private to the package
## Variadic functions
Functions that accept a variable number of arguments
.play -edit code/variadic/variadic.go
## Deferred execution
You can execute a function call after the termination of the surrounding function.
Arguments are evaluated immediately.
.play -edit code/defer/defer.go
Good for cleanup, e.g. closing files.
## Pointers
If you are familiar with C/C++ no problem!
## Pointers
.play -edit code/pointers/pointers.go /Decl/,/END OMIT/
## Arrays and Slices
## Arrays
Arrays have fixed length. Go's arrays are values. Not pointers like in C. When passed as a parameter to
a function a copy of the array is created.
.play -edit code/arrays/arrays.go
## Slices
Slices have no fixed size. They are dynamically resized.
A slice is a struct with an array, a length and a capacity.
```
type slice struct {
elements unsafe.Pointer
len int
cap int
}
```
A slice can be created with:
```
slice := make([]int, 5)
```
or with a slice literal:
```
slice := []{1,2,3}
## Slices
.play -edit code/slices/slices.go /workOnSlice/,/END/
## Functional aspects
## Functions as values
.play -edit code/eval/eval.go /M OMIT/,/END OMIT/
## Currying
A function returning a function (closure)
.play -edit code/currying/currying.go
## Go is Object-Oriented (?)
## Struct types instead of classes
.play -edit code/square/square.go /Square OMIT/,/END OMIT/
## Interfaces
Interfaces are collections of method signatures.
.code code/interface/interface.go /interface/,/}/
Interfaces are **contracts**. Any user-defined type providing the methods of an interface **implicitly** implements the interface.
.code code/interface/interface.go /Square/,/INT/
`*Square` implements `Shape`
## Interfaces (example)
#.code player/player.go /INTER OMIT/,/END OMIT/
.play code/interface/interface.go /Shape/,/END/
## Composition (Embedding) instead of Inheritance (It's not!)
.play -edit code/oop/shape.go /type Rectangle/,/END OMIT/
## Error Handling
## Errors as function results - Instead of exceptions
Errors (implicitly) implement the error interface, by defining the Error() method.
.code -edit code/error/error.go /ERROR OMIT/,/END OMIT/
Functions may return errors
.code -edit code/error/error.go /DIV OMIT/,/END OMIT/
## Checking for errors
.play -edit code/error/error.go /CHECK OMIT/,/END OMIT/
## Testing
## Testing
Test files end in `_test.go`.
They can be in the same package with the source file or in a different package.
No assertions. Full manual control. Test functions have the form
```
func TestFoo(t *testing.T) {
...
}
```
`go test ./...` executes all tests in all folders and subfolders.
// .code -edit code/factorial/factorial_test.go /TEST OMIT/,/END OMIT/
.code -edit code/factorial/factorial_test.go
## Building a REST API
## REST API Routes
Using a 3rd party HTTP router and dispatcher <https://github.com/gorilla/mux>
#.play -edit code/restapi/restapi.go
.play -edit code/restapi/restapi.go /ROUTE OMIT/,/END OMIT/
.link http://localhost:8080/api/products
.link http://localhost:8080/api/product/cof
## REST API - JSON Encoding
.code -edit code/restapi/restapi.go /PROD OMIT/,/END OMIT/
## REST API - Getting a product
.code -edit code/restapi/restapi.go /GET OMIT/,/END OMIT/
.code -edit code/restapi/restapi.go /RESPOND OMIT/,/END OMIT/
## Concurrency
## Goroutines
.play -edit code/goroutines/goroutines.go /CODE OMIT/,/END OMIT/
## Communication
Go follows the CSP (Communicating Sequential Processes) semantics.
Communication is synchronous (blocking)
Write a value to a channel
.code code/clock/clock.go /WRITE OMIT/,/END OMIT/
Read a value from a channel (and assign to a variable)
.code -edit code/clock/clock.go /READ OMIT/,/END OMIT/
Spawning a goroutine
.code -edit code/clock/clock.go /GO OMIT/,/END OMIT/
## Example of synchronous channel communication
.play -edit code/clock/clock.go /CODE OMIT/,/ENDCODE OMIT/
## Tasks
Mock Tasks
.code -edit code/tasks/tasks_s.go /Task OMIT/,/END OMIT/
Work on task
.code -edit code/tasks/tasks_s.go /Work OMIT/,/}/
## Worker
Channels
.code -edit code/tasks/tasks_s.go /CHAN OMIT/,/END OMIT/
Worker
.code -edit code/tasks/tasks_s.go /WORKER OMIT/,/END OMIT/
## Worker pool (cont)
.play -edit code/tasks/tasks_s.go /MAIN OMIT/,/END OMIT/
## Consume from many producers
Producer
.code -edit code/producer/producer.go /PROD OMIT/,/END OMIT/
## Consumer (Select statement)
.code -edit code/producer/producer.go /CONS OMIT/,/END OMIT/
.play -edit code/producer/producer.go /func main/,/}/