-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgentlego.slide
682 lines (387 loc) · 11 KB
/
gentlego.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
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
A Gentle Intro to Go
22 Feb 2014
Fred Alger
Increasing the Bus Factor at FoxyCart
https://github.com/phred
@_phred
* Goals
- To give you a taste of Go...
- So you can get addicted like me. :)
* Goals (cont.)
- Compare and contrast Go & Python strengths
- Show ways that Go might help your project
* Me
- Polyglot at heart
- BASIC → Visual Basic → C → C++ → PHP → SQL → Javascript → Java → Python → Ruby → Bash → Lasso (briefly) → Factor → Forth → Go
* Me
- "the right tool for the job"
- Like simple things.
* Nowadays
- Do a lot of system administration.
- Spread the knowledge
* What do you call someone who writes Go code?
* What do you call someone who writes Go code?
.image gopher.png
* "So a Gopher goes to a Python convention..."
* So what's the deal?
- Just another programming lanugage?
- Something new.
* Why I think it's something new
- whitespace like a pythonista
- elegant like a rubyist
- async like a node.js junkie
- concurrent like an erlanger
- typesafe and GC'd like a Javabro
- minimal like a C guru
.image gopher.png
* What is Go?
- Google
- Rob Pike (Plan 9) / Ken Thompson (B, UNIX) / Robert Griesemer (V8 / Chubby)
- ~5 years old
- Surprisingly mature
.image gopher.png
* Go?
- Also "Golang"
- Statically typed
- Compiled
- Concurrent
* Go?
- A stable language specification (v1.2 as of today)
- A runtime
- A suite of tools (`go` `build`, `go` `get`, `go` `install`, `go` `doc`)
* Hello Go
.play hello.go
* Hello Python
.code hello.py
* Hello Python
.code hello.py
BUT WAIT
SyntaxError: Non-ASCII character '\xe4' in file hello.py on line 3, but no encoding declared;
see http://www.python.org/peps/pep-0263.html for details
* Hello Python 2
.code hello2.py
* What's great!
* What's great about Python
- simple
- readable
- clean
* What's great about Go: same values
- Result: Programmer productivity.
* What's great about Go: same values
Simple
- concepts are easy to understand
- (the implementation might still be sophisticated)
Orthogonal
- concepts mix cleanly
- easy to understand and predict what happens
Succinct
- no need to predeclare every intention
Safe
- misbehavior should be detected
.link http://gotocon.com/dl/jaoo-aarhus-2010/slides/RobPike_KeynoteTheExpressivenessOfGo.pdf
* Familiar?
* Python philosophy
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
...
* What's great about Python? (cont.)
- Interactive development / REPL
* But... Go is compiled!
* Go is compiled
- No REPL
1. Edit code
2. `go` `build` `test.go`
3. ``./test`
* Go is compiled
- Compiled, but compiles quickly
- Extremely fast, build the whole Go compiler in < 10 seconds
- Compiling prevents errors
* Compiler
Less of this:
.image compiling.png
.link http://xkcd.com/303/
* Compiled (cont.)
- Ways around save/compile/run
- e.g., Revel [[http://robfig.github.io/revel/]]
* What great about Python (cont.)
- "Batteries included" standard library
* Go's standard library
- Web-oriented
- System calls
- Encryption
- Compression / decompression
- Templating
- Production-grade web server
* Go standard library
.play serve.go
.link http://localhost:3434/pytn
* Go web server
- Production ready
* Go web server
- Chunked encoding
- Keepalives
- Sandboxing
- Battle tested, DoS hardened
* Easy deployment
- No nginx, copy the binary and run it
* What's great about Python?
- Built-in Docs
- Generated docs
* Godoc
.link http://godoc.dev/pkg/net/http
.link http://godoc.dev/pkg/net/http/#Handler
* Godoc
- Works from the CLI too
- Interactive examples on [[http://golang.org]]
* What's great about Python (cont.)
- Community, PSR
- Made by smart people like YOU
* Go community
- Small, active
- Full of smart, opinionated people
* What could be better
* What could be better (cont.)
- Deployment — pip, virtualenv
- `2.6` vs `2.7` vs `3k`
- Needs "legs" — WSGI, worker pool, pool monitoring
* Go awesome: deployment
- Copy the binary
- Run the binary
- Love the binary
* What could be better (cont.)
- Interpreter startup
- Especially on low-end systems (Raspberry Pi)
* Go awesome: cross compilation
- Cross-compiling
goxc -bc="linux,arm"
- Builds arm binaries & deb/rpm packages
.link https://github.com/laher/goxc
* What could be better
- I'm actually OK with the GIL
- There are good reasons _for_ it (C extensions)
- But...
* What could be better (cont.)
- It's bad for multi-threading, especially in CPU-bound programs.
- ^C a multi-threaded Python program? Good luck.
Gory details:
.link http://www.dabeaz.com/python/GIL.pdf
* What's great about Go
- FAST
- Cheap concurrency
* FAST
.image Benchmark1.png
* FAST (cont.)
.image Benchmark2.png
* Concurrency vs. parallelism
- Threads vs. greenlets
- Go implements CSP
.link http://en.wikipedia.org/wiki/Communicating_sequential_processes
* Concurrency vs. parallelism
- "Share by Communicating"
- Channels and goroutines vs. threads and locks/mutexes
- Nice abstraction
* Concurrency vs. parallelism
.play chans.go
* Concurrency vs. parallelism
- Moore's Law
- Parallelism is the future
- Runtime support
export GOMAXPROCS=8 # number of CPUs
* More Go Strengths
* What's weird about Go
* More Go Strengths
* Strong philosophy
- Simple / Orthogonal / Succinct / Safe
- Programmer productivity
- Fun.
* Simple: Clean syntax
- Few types and keywords (only 25)
- Smaller than most languages
* Clean
.image NumKw.png
.link http://gotocon.com/dl/jaoo-aarhus-2010/slides/RobPike_KeynoteTheExpressivenessOfGo.pdf
* Simple: Automatic formatting
- The standard for how Go code should be formatted?
- The output of the `go` `fmt` tool.
- PEP-8 on steroids
* Safe: Compiler (cont.)
- Forces clean code
- Static checks are nice
* Compiler (cont.)
Unused variables:
.play unusedvar.go
* Compiler (cont.)
Fat-fingered mistakes:
.play fatfinger.go
* Compiler (cont.)
Monkey Business:
.play monkeybiz.go
* Compiler (cont.)
Type safety:
.play -edit typesafe.go
* Compiler (cont.)
- Stops you from writing scummy code.
* Compiler
- Checks yourself before you wrecks yourself
* What's weird about Go
* What's weird about Go
Pascal-style declarations
var count int
var samples []int // Brackets first, really
func sum(array []int) int { // Return type at the end
// ...
}
NOT
int count
int samples[]
int sum(int array[]) {
}
* What's weird (cont.)
Pascal-style declarations
var count int
var samples []int
count := 1 // "Declare and initialize"
samples := make([]int) // Type is inferred from RHS
"You shouldn't have to repeat things that the compiler already knows or can guess."
* What's weird (cont.)
The brackets thing is weird, I'm still not 100% used to it.
* What's weird (cont.)
Unused imports are an *error*
.play -edit unused.go
* What's weird (cont.)
Module visibility is specified by upper or lower case.
package gravitron
func notExported() {
// ...
}
func Exported() {
// ...
}
* What's weird (cont.)
- Object oriented, sort of
- Interfaces
- No inheritance, only embedding
* Actually, Interfaces are nice
- Duck typing that gets checked
* Actually, Interfaces are nice
- Interfaces specify "I need a thing that does this"
type Reader interface {
Read(p []byte) (n int, err error)
}
func ReadData(reader io.Reader) {
}
* Actually, Interfaces are nice
Lots of things provide the Reader interface:
- Crypto algorithms
- Network sockets
- HTTP Requests
- MIME data
* Actually, Interfaces are nice
Lots of things use them as well:
- JSON decoder
- `fmt`, Printf / Println to anywhere
* Neat applications
* Signals in Python
Callbacks
.code signals.py
* Signals in Go
No Callbacks
.play signals.go /func main/,/^}/
* Application: Cheap Concurrency
- Little overhead
- Create thousands of goroutines every second
- Expressive patterns
* Concurrency
Working with a web API
.code api_sync.go /^func testUsers/,/^}/
* Concurrency (cont.)
- Pretty common in a test suite
- Slow
- Threaded? Could be annoying.
* Concurrency (cont.)
- We can do better.
* Concurrency (cont.)
.code api_async.go /^func testUsers/,/^}/ HLinit
* Concurrency (cont.)
.code api_async.go /^func testUsers/,/^}/ HLspawn
* Concurrency (cont.)
.code api_async.go /^func testUsers/,/^}/ HLcollect
* Concurrency (cont.)
- Let your program's logic dictate the flow, not threading / callback logic.
- Feels very natural.
* Further Applications
- Tiny Servers
- Data collection
- Notifications
- Workers
* Application: Tiny Servers
* Application: Tiny Servers
- CloudFlare's Railgun: http://blog.cloudflare.com/go-at-cloudflare
- HTTP proxy server
* Application: Tiny Servers
- Compressed / delta / multiplexed replies
- Run on your server
- CloudFlare edge talks to it directly
* Application: Tiny Servers
- Unique approach
- Big performance benefits
- Go features: speed, reliability, compiled binaries
* Application: Data Collection
* Application: Data Collection
- `bit.ly` wrote a Go implementation of etsy's `statsd`:
https://github.com/bitly/statsdaemon
- Ad-hoc statistics and metrics for large data volumes.
- Simple to deploy.
* Application: Notifications
* Application: Notifications
- Websocket / real-time
import "code.google.com/p/go.net/websocket"
- Redis
- MongoDB
- Message queues (RabbitMQ)
* Application: Notifications
- bit.ly wrote their own "distributed realtime messaging at scale" [[http://word.bitly.com/post/33232969144/nsq]]
- Python and Go clients
- Go server
* Application: Notifications
Regarding NSQ, Go channels (not to be confused with NSQ channels)
and the language’s built in concurrency features are a perfect fit
for the internal workings of nsqd. We leverage buffered channels
to manage our in memory message queues and seamlessly write
overflow to disk.
.link http://word.bitly.com/post/33232969144/nsq
* Application: Workers
* Application: Workers
- Very fast to start up and begin work
- Efficient on cloud environments
- Nice database and message queue libraries
* Application: Distributed Data Stores
* Application: Distributed Data Stores
- Various key-value stores, many Redis-like
- `CoreOS` / etcd
* Application: Distributed Data Stores
- HTTP API for configuration information
- Failover, service coordination
* Application: Distributed Data Stores
- Implementation of Raft algorithm
- Strong CAP guarantees
import "https://github.com/goraft/raft"
* Conclusion
* Conclusion
- Go and Python share a lot of values
- Go has some unique strengths
- Plenty of real-world applications
- Might be a good fit for your next project!
* Where to Go from here
- Go homepage [[http://golang.org]]
- Go Tour [[http://golang.org/tour]]
- Interactive samples, no download required