forked from emilevauge/go-lyra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
702 lines (559 loc) · 13.2 KB
/
index.html
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
<!--
slidedeck: A modification of the Google IO 2012 HTML5 slide template
URL: https://github.com/rmcgibbo/slidedeck
Based on https://github.com/francescolaffi/elastic-google-io-slides, and
ultimately:
Google IO 2012 HTML5 Slide Template
Authors: Eric Bidelman <[email protected]>
Luke Mahe <[email protected]>
URL: https://code.google.com/p/io-2012-slides
-->
<!DOCTYPE html>
<html>
<head>
<title> Workshop GO</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">-->
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0">-->
<!--This one seems to work all the time, but really small on ipad-->
<!--<meta name="viewport" content="initial-scale=0.4">-->
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="stylesheet" media="all" href="theme/css/default.css">
<link rel="stylesheet" media="all" href="theme/css/custom.css">
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="theme/css/phone.css">
<base target="_blank"> <!-- This amazingness opens all links in a new tab. -->
<script data-main="js/slides", src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.min.js"></script>
<!-- MathJax support -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js"],
jax: ["input/TeX", "output/HTML-CSS"],
showProcessingMessages: false,
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEscapes: true
},
TeX: {
extensions: ["color.js"]
},
"HTML-CSS": { availableFonts: ["TeX"] }
});
</script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<div style="display:hidden">
\[
\definecolor{data}{RGB}{18,110,213}
\definecolor{unknown}{RGB}{217,86,16}
\definecolor{learned}{RGB}{175,114,176}
\]
</div>
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<slide class="title-slide segue nobackground">
<hgroup class="auto-fadein">
<h1><img height=300 src=figures/golang.png /></h1>
<h2> Workshop GO</h2>
<p> Emile Vauge</p>
</hgroup>
</slide>
<slide >
<hgroup>
<h2>Setup</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li>On GitHub, fork <a href="https://github.com/emilevauge/go-lyra">https://github.com/emilevauge/go-lyra</a></li>
<li>Go to <a href="http://c9.io">http://c9.io</a></li>
<li>Sign in using your GitHub account</li>
<li>In Cloud9, edit this repository</li>
<li>Let's code !</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>GO in few words</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li>designed by Google (Robert Griesemer, Rob Pike, and Ken Thompson)</li>
<li>open source</li>
<li>commonly called <strong>golang</strong></li>
<li>simple, productive, readable</li>
<li>a modern take on C</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>more words...</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li>fast (compile, run)</li>
<li>garbage collected</li>
<li>strongly typed</li>
<li>statically linked binaries</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>Where is the catch?</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li>still early (1.0 in March 2012)</li>
<li>dependencies management</li>
<li>no generics (yet)</li>
<li>not really OOP</li>
<li>lack of powerful IDE</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>Here and there</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li>Google (obviously)</li>
<li>Docker</li>
<li>Twitter</li>
<li>AirBnB</li>
<li>SoundCloud</li>
<li>Netflix</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>Tools</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li><code>go build</code>: which builds Go binaries</li>
<li><code>go test</code>: for unit testing and microbenchmarks</li>
<li><code>go fmt</code>: for formatting code</li>
<li><code>go run</code>: a shortcut for building and executing code</li>
<li><code>godoc</code>: for displaying documentation or serving it via HTTP</li>
<li><code>go generate</code>: a standard way to invoke code generators</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>Hello, Playground!</h2>
<h3></h3>
</hgroup>
<article ><p><a href="http://play.golang.org/">http://play.golang.org/</a></p>
<pre class="prettyprint" data-lang="go">
package main
import "fmt"
func main() {
fmt.Println("Hello, playground")
}
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Main function</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li>Every go program has a main() function</li>
<li>This is where execution starts</li>
</ul>
<pre class="prettyprint" data-lang="go">
package main
func main() {
// Your program starts here
}
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Output</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li>fmt is a core package (means “format”)</li>
<li>pronounced "fumpt" (weird)</li>
</ul>
<pre class="prettyprint" data-lang="go">
package main
import "fmt"
func main() {
fmt.Println("Hello Lyra!")
}
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Exercise `1_output`</h2>
<h3></h3>
</hgroup>
<article ></article>
</slide>
<slide >
<hgroup>
<h2>Variables</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li>Define</li>
</ul>
<pre class="prettyprint" data-lang="go">
var myVar string
</pre>
<ul>
<li>Initialize</li>
</ul>
<pre class="prettyprint" data-lang="go">
var myVar string
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Type Inference</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li>Define and Initialize</li>
</ul>
<pre class="prettyprint" data-lang="go">
var myVar string
</pre>
<ul>
<li>Initialize</li>
</ul>
<pre class="prettyprint" data-lang="go">
myVar := "Hello"
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Functions</h2>
<h3></h3>
</hgroup>
<article ><pre class="prettyprint" data-lang="go">
func withArgs(a int, b string) int {
return 3
}
result := withArgs(3, "Hello")
</pre>
<pre class="prettyprint" data-lang="go">
func withMultipleReturns() (string, error) {
return "Hello", nil
}
s, err := withMultipleReturns()
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Exercise `4_returns`</h2>
<h3></h3>
</hgroup>
<article ></article>
</slide>
<slide >
<hgroup>
<h2>Structs</h2>
<h3>Define</h3>
</hgroup>
<article ><pre class="prettyprint" data-lang="go">
type Person struct {
Name string
Age int
}
p := Person{
Name: "Greg",
Age: 25,
}
fmt.Println(p.Name)
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Structs</h2>
<h3>Access</h3>
</hgroup>
<article ><ul>
<li>public Properties Are Uppercase</li>
</ul>
<p><em>accessible to anything</em></p>
<ul>
<li>private properties are lowercase</li>
</ul>
<p><em>only accessible via methods</em></p></article>
</slide>
<slide >
<hgroup>
<h2>Structs</h2>
<h3>Instantiation</h3>
</hgroup>
<article ><pre class="prettyprint" data-lang="go">
type Person struct {
Name string
Age int
}
p := Person{
Name: "Greg",
Age: 25,
}
fmt.Println(p.Name)
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Structs</h2>
<h3>Methods</h3>
</hgroup>
<article ><pre class="prettyprint" data-lang="go">
type Person struct {
Name string
Age int
}
func (p *Person) CanEnterPub() bool {
return p.Age >= 19
}
person.CanEnterPub()
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Exercise `6_methods`</h2>
<h3></h3>
</hgroup>
<article ></article>
</slide>
<slide >
<hgroup>
<h2>GoRoutines</h2>
<h3></h3>
</hgroup>
<article ><p>Goroutines are lightweight threads of execution</p>
<ul>
<li>executed concurrently</li>
<li>supports > 100,000 goroutines</li>
<li>automatically distributes load across CPUs</li>
<li>Worry free!</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>GoRoutines</h2>
<h3>How?</h3>
</hgroup>
<article ><pre class="prettyprint" data-lang="go">
doSomething()
</pre>
<pre class="prettyprint" data-lang="go">
go doSomething()
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Channels</h2>
<h3></h3>
</hgroup>
<article ><p>Goroutines communicate via channels: one-way pipes
Operations:</p>
<ul>
<li>Push to channel</li>
<li>Listen to channel</li>
</ul>
<p>Channels have types too:</p>
<ul>
<li>var chan int</li>
<li>var chan string</li>
<li>var chan map[string]MyStruct</li>
</ul>
<p>Use channels to share state, for control flow</p></article>
</slide>
<slide >
<hgroup>
<h2>Channels</h2>
<h3>Pushing</h3>
</hgroup>
<article ><ul>
<li>
<p>Use <code>make()</code> to create a channel
<pre class="prettyprint" data-lang="go">
var channel := make(chan int)
</pre></p>
</li>
<li>
<p>Push to a channel with <code><-</code>
<pre class="prettyprint" data-lang="go">
channel <- 5
channel <- 3
</pre></p>
</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>Channels</h2>
<h3>Listening</h3>
</hgroup>
<article ><ul>
<li>Use select to wait (block) for a single message</li>
<li>
<p>Similar syntax to case
<pre class="prettyprint" data-lang="go">
select {
case i <- channel:
fmt.Printf("Got message %d", i)
}
</pre></p>
</li>
<li>
<p>Watch for multiple messages using range
<pre class="prettyprint" data-lang="go">
for i := range channel {
fmt.Printf("Got message %d", i)
}
</pre></p>
</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>Exercise `9_channels`</h2>
<h3></h3>
</hgroup>
<article ></article>
</slide>
<slide >
<hgroup>
<h2>Loops</h2>
<h3></h3>
</hgroup>
<article ><pre class="prettyprint" data-lang="go">
// for loop
for i := 0; i < 10; i++ {...}
// while loop
for i < 10 {...}
// infinite loop
for {...}
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Web</h2>
<h3></h3>
</hgroup>
<article ><pre class="prettyprint" data-lang="go">
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Exercise `12_web`</h2>
<h3></h3>
</hgroup>
<article ></article>
</slide>
<slide >
<hgroup>
<h2>Gorilla</h2>
<h3>Advanced routing</h3>
</hgroup>
<article ><ul>
<li>URL host</li>
<li>path</li>
<li>path prefix</li>
<li>schemes</li>
<li>header</li>
<li>query values</li>
<li>HTTP methods</li>
<li>custom matchers</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>Gorilla</h2>
<h3>Example</h3>
</hgroup>
<article ><pre class="prettyprint" data-lang="go">
func main() {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/products", ProductsHandler)
r.HandleFunc("/articles", ArticlesHandler)
http.ListenAndServe(":8080", r)
}
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Gorilla</h2>
<h3>Variables</h3>
</hgroup>
<article ><pre class="prettyprint" data-lang="go">
r := mux.NewRouter()
r.HandleFunc("/products/{key}", ProductHandler)
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
</pre>
<pre class="prettyprint" data-lang="go">
vars := mux.Vars(request)
category := vars["category"]
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Gorilla</h2>
<h3>Domain, Dynamic sub-domain</h3>
</hgroup>
<article ><pre class="prettyprint" data-lang="go">
r := mux.NewRouter()
// Only matches if domain is "www.example.com".
r.Host("www.example.com")
// Matches a dynamic subdomain.
r.Host("{subdomain:[a-z]+}.domain.com")
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Exercise `13_rest`</h2>
<h3></h3>
</hgroup>
<article ></article>
</slide>
<slide class="thank-you-slide segue nobackground">
<!-- <aside class="gdbar right"><img src="images/google_developers_icon_128.png"></aside> -->
<article class="flexbox vleft auto-fadein">
<h2> Thank you!</h2>
<p></p>
</article>
<p data-config-contact class="auto-fadein"> <a href="https://twitter.com/emilevauge">@emilevauge</a></p>
</p>
</slide>
<slide class="backdrop"></slide>
</slides>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!--[if IE]>
<script src="http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js"></script>
<script>CFInstall.check({mode: 'overlay'});</script>
<![endif]-->
</body>
</html>