-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtalk.slide
431 lines (222 loc) · 8.12 KB
/
talk.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
Golang in an hour
A talk for Torino Coding Society
11 May 2015
Gianfranco Reppucci
Software developer, Qurami
gianfranco.reppucci@qurami.net
http://qurami.com
@giefferre
* Hello World!
.image img/giefferre.jpg 250 _
- Software developer [[http://qurami.com][@Qurami]]
- Founder [[http://meetup.com/AppsterdamMilan][@AppsterdamMilan]]
- Golang developer & enthusiast
* Disclaimer
Code crusaders? No, thanks
.image img/codecrusader.jpg 400 _
* Agenda
- Introduction
- Basics
- Go is about composition
- Parallelism and synchronization
- What I learned
- Tips & Tricks
- Reference
* Introduction
* Development evolution
- From _monolithic_code_ to _microservices_
- From "all-inclusive frameworks" to bootstrapping frameworks with dependency managers
- From C to ?
* Context
- There is a lot of different programming languages available to developers
- Some of them are pretty easy, some others have great performances
- We can find frameworks, plugins and extensions for each of them
*
.image img/indiana.jpg 520 _
* Start using something modern
.image img/gopher_yellow.png 300 _
.caption *Go*is*an*open*source*programming*language*
.caption *that*makes*it*easy*to*build*simple,*reliable*and*efficient*software.*
.caption quote from golang.org
* History
- *2007*: first experiment
- *2009*: public launch
- *2011*: integration in Google App Engine
- *2012*: release of version 1
- *2015*: support for mobile devices _(_v._>=_1.5)_
* Key points
- Strongly typed language, featuring advanced tools
- Absolutely genuine: syntax and environments are similar to dynamic languages
- Performance driven
- It's open sourced, it is secure
* Batteries included
- Lightweight runtime components (garbage collector, scheduler, etc.)
- Two compilers (gc or gccgo)
- Plenty of standard libraries
- Documentation
- Formatter
* Formatter
From this...
.image img/code_mess.png 200 _
To this :)
.image img/code_clean.png 200 _
*
.image img/biplane.jpg 300 _
.caption *Go*sits*somewhere*between*C*and*Python.*
.caption *It*has*the*static*type*checking*and*bit-twiddling*powers*of*C,*
.caption *yet*much*of*the*speed*of*development*and*conciseness*of*Python*
.caption quote from Graham King
* Basics
* First things first
.play code/helloworld.go
* First things first (cont.)
.code code/helloworld.go
- _package_ determines the namespace
- it CANNOT be omitted
- if you are not writing a package, it shall be *main*
* First things first (cont.)
import "fmt"
import (
"fmt"
"net/http"
"code.google.com/p/goconf/conf"
"github.com/garyburd/redigo/redis"
)
- _import_ loads one or more packages
- packages could be imported even remotely, from Googlecode or Github
* First things first (cont.)
.code code/helloworld.go
- fmt is a _formatting_ library
* Variables and scope
.code code/variables.go /START2/,/STOP2/
is equivalent to:
.code code/variables.go /START1/,/STOP1/
Check it out:
.play code/variables.go /START3/,/STOP3/
* Variables and scope (cont.)
Be aware of the scope!
.play code/scope.go /START1/,/STOP1/
* Methods
Methods are declared with the keyword *func* as follows:
.play code/methods.go /START1/,/STOP1/
* Error handling
There is no exception, only errors
file, err := os.Open(filePath)
if err != nil {
return err
}
codeUsing(file)
If you forget to handle and error, the application will probably crash!
* Go is about composition
* Structs
While Java or C are about _inheritance_, Go is about _composition_
Go is Object Oriented, but not in the usual way
In fact:
- No class inheritance
- No method or operator overloading
- No circular dependencies among packages
*
.image img/zak.jpg 520 _
* The Person struct
Let's define the struct _Person_
.code code/structs.go /START1/,/STOP1/
With its own methods...
.code code/structs.go /START2/,/STOP2/
* The Person struct (cont.)
And of course a constructor-like factory method:
.code code/structs.go /START3/,/STOP3/
* The Person struct (cont.)
We'll use the struct Person as follows:
.code code/structs.go /START7/,/STOP7/
.code code/structs.go /START8/,/STOP8/
*
.image img/bernard.png 520 _
* The Student struct
Let's *compose* a new struct _Student_ by using the previous struct:
.code code/structs.go /START4/,/STOP4/
.code code/structs.go /START5/,/STOP5/
* The Student struct (cont.)
And constructor as usual:
.code code/structs.go /START6/,/STOP6/
* Putting it all together
As simple as that:
.play code/structs.go /START9/,/STOP9/
* Parallelism and synchronization
*
.image img/parallelism.jpg 520 _
* Goroutines
A _goroutine_ is a lightweight thread.
To launch a goroutine, use the *go* keyword before a function.
.play code/goroutines.go /START1/,/STOP1/
* Goroutines (cont.)
Goroutines can also use anonymouse methods
.play code/goroutines_anon.go /START1/,/STOP1/
* Channels
Channels allow you to pass data between running goroutines.
They can be _buffered_ or _unbuffered_.
You can build a channel from any kind of data structure!
ch := make(chan int)
// insert a value into the channel
ch <- 5
// read data from the channel
var readValue int
readValue = <-ch
// readValue is 5
* The beacon
.play code/channels.go /START1/,/STOP1/
* Select
The _select_ statement is like a switch statement, but it selects over channel operations using a first-come, first-served logic.
It is useful to implement synchronization!
* The beacon, using select
.play code/select.go /START1/,/STOP1/
* What I learned
* PROs
- Explicit is better than implicit: Go code is _really_readable_
- Concurrency is super-easy
- Your application ships within a single binary file - no extra dependencies!
- Your application runs on (almost) any platform
* Warnings
- Be aware of error handling
- Sometimes you'll write real "low-level" code
* CONs
No generics, sorry!
There are two ways to decode JSON, XML or objects in other formats to Golang structs:
- If you know the exact model, via the _Unmarshal_ method
- If you don't know the attributes of your model, do _type_assertion_
* CONs (cont.)
Package versioning is not handled at all :(
An update of a remote package could break compatibility!
Your application will not build anymore
* From an entrepreneur's point of view
- The language is born and supported from Google
- [[https://github.com/golang/go/wiki/GoUsers][The list of Golang users is growing up extremely fast]], you're not alone
- The learning curve is quite high, but you'll have better programmers
* Tips & Tricks
* Test suite
Care about *TDD* (_you_should!_)?
You can use the *go*test* native tool, or use a more advanced suite like *Goconvey*
Project site is [[http://goconvey.co]]
* Installing multiple Go versions
Do you need to use different versions of Go?
Do not mess your environment, use the *Go*Version*Manager* tool!
Instructions are available at [[https://github.com/moovweb/gvm]]
* Want to create a keynote like this?
Use the *present* tool!
It is a native golang solution, available as a package, to run slides and code within your browser.
Check it out at [[https://godoc.org/golang.org/x/tools/present]]
* Golang hands on, within your browser
Start using Go easily!
Point your browser to [[http://play.golang.org]].
.image img/playground.png 400 _
* Check this keynote later
Bookmark this address
[[http://go-talks.appspot.com/github.com/giefferre/golang-in-an-hour/talk.slide]]
* Reference
.link http://reneefrench.blogspot.com The gopher images, by Renee French
.link http://matt.aimonetti.net/posts/2013/08/27/what-technology-should-my-startup-use What technology should my startup use?, by Matt Aimonetti
.link http://www.darkcoding.net/software/go-lang-after-four-months/ Go after four months, by Graham King
.link http://blog.iron.io/2013/03/how-we-went-from-30-servers-to-2-go.html How we went from 30 servers to 2, by Travis Reeder
.link http://blog.iron.io/2013/08/go-after-2-years-in-production.html Go after 2 years in production, by Travis Reeder
.link http://www.slideshare.net/jpetazzo/docker-and-go-why-did-we-decide-to-write-docker-in-go Docker and Go: why did we decide to write docker in Go?
*
.image img/the_end.jpg 520 _