-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrating-from-python.slide
372 lines (214 loc) · 7.93 KB
/
migrating-from-python.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
Migrating to Go from Python
Brian G. Merrell
Fungineer, Rakuten MediaForge
bgmerrell (Twitter, Gmail, GitHub, etc)
* Goal of this talk
Understand trade-offs between Go and Python development
.image migrating-from-python/img/gopher.png
* Somewhat autobiographical
Story time...
* What I like about Python
- Easy-to-read syntax
- Powerful standard library
- Fun to write
- Get a lot done very quickly
- Often fast enough (in terms of execution speed)
* Justification -- Why another language?
"Programming had become too difficult"
Many programmers were choosing ease over safety and efficiency
The computing landscape and scale has changed
C++ is too complex
Compilation is too slow
*
.image migrating-from-python/img/compiling.png 550 550
*
.image migrating-from-python/img/flying-with-python.png
* What I dislike about Python
Actually quite complex
Runtime errors
It doesn't scale as well as I would like...
- with team size (many ways to do the same thing, pep8)
- with codebase size (runtime errors, type hierarchies, magic)
- with increasing performance demands (GIL, lack of concurrency primitives, interpreted)
*
.image migrating-from-python/img/flying-with-go.jpg
* What I like about Go
- Small language (it fits in your head; I've read the spec)
- Compile time checks (fast!)
- Fast enough for even more things
- Benefits come at very little cost
- Dynamic feel with compiled performance
- Rich standard library
- Libraries are all written in Go
- Great bundled tooling
- Seems to scale well (TBD)
* Why people are using Go
- Faster (Big data, web services)
- Safer (Compile time checks, built-in concurrency primitives)
- Productiver (Rich tools, compositional coding, less is more[1])
All this comes at almost no cost vs. a dynamic language
Rakuten MediaForge changed for the first reason
[1] [[http://commandcenter.blogspot.com/2012/06/less-is-exponentially-more.html]]
* Hello World
.play migrating-from-python/hello-world-web.go
* Benchmarks
.image migrating-from-python/img/go-python-benchmark.png 462 740
* What I dislike about Go
- Naked return statements
.code migrating-from-python/img/naked-return.go
- At case where I wish scoping rules were different
- Not faster than Java... yet
- new vs. &T{} vs. make
- len("foo") vs. "foo".Len() (but it keeps the language simpler)
- The name "Go" is ungoogleable / hashtagable (use golang)
Definitely no deal breakers
* Who is using Go?
.image migrating-from-python/img/company-logos.jpg 623 977
* Who is using Go in Utah?
.image migrating-from-python/img/utah-company-logos.jpg 623 977
* Gophercon 2014
"After doing the Go tour, he felt competent enough to fix an issue. That wouldn't have happened with Python or Ruby."
"Adding new features can weaken your existing features."
"Before the 1.0 release, *everything* was revisited with a critical eye, and it was asked 'What can we remove?'"
* The High-Level View
.image migrating-from-python/img/python-vs-go-table.png
* Go does not have...
Classes
Exceptions
Assertions
Templates / generics (for now)
Operator overloading
* Learning a new language
"...A straightforward translation of a C++ or Java program into Go is unlikely to produce a satisfactory result—Java programs are written in Java, not Go. [...] To write Go well, it's important to understand its properties and idioms. It's also important to know the established conventions for programming in Go, such as naming, formatting, program construction, and so on, so that programs you write will be easy for other Go programmers to understand."
Effective Go ([[http://golang.org/doc/effective_go.html]])
* Zen of Python
.code migrating-from-python/zen.txt
* Explicit is better than implicit
.play migrating-from-python/set-and-get-attr.py /START OMIT/,/END OMIT/
* Explicit is better than implicit
.play migrating-from-python/set-and-get-attr.py
* Simple is better than complex
Python is actually quite complex under the surface
Go does not have
- static methods
- class methods
- properties
- iterators
- generators
- exceptions
- meta classes
- class decorators
- multiple inheritance
- default argument values
- decorators (but I sure tried!)
* Instead of iterators...
.code migrating-from-python/leveldb-iter.go
* Flat is better than nested
.play migrating-from-python/nested.py
* Flat is better than nested
.play migrating-from-python/flat.go /START OMIT/,/END OMIT/
* Sparse is better than dense
- No list comprehensions
.code migrating-from-python/one-liner-2.py
.code migrating-from-python/one-liner-3.py
- No ternary operator (expression?true:false)
.code migrating-from-python/one-liner-1.py
* Readability counts
Python
- Dense code
- Magic
- Lots of syntactic sugar
Go
- Small language
- Very little syntactic sugar
- Very explicit
- No operator overloading
* Errors should never pass silently (unless explicitly silenced)
.play migrating-from-python/errors.go
This sure beats a runtime error
* Zero values
.play migrating-from-python/zero-value.py
.play migrating-from-python/zero-value.go /START OMIT/,/END OMIT/
* Constructors & composite literals
.play migrating-from-python/constructors.go /START OMIT/,/END OMIT/
Remember field values can be specified during instantiation...
.play migrating-from-python/constructors-2.go /START OMIT/,/END OMIT/
* Encapsulation
Capitalized names are exported
.code migrating-from-python/buffer-struct.go
[[http://golang.org/pkg/bytes/#Buffer]]
.play migrating-from-python/encapsulation.go /START OMIT/,/END OMIT/
* Slices vs. Lists
.play migrating-from-python/slices.go /START OMIT/,/END OMIT/
.play migrating-from-python/lists.py
* Arrays
.play migrating-from-python/arrays.go /START OMIT/,/END OMIT/
* Slicing
.play migrating-from-python/slicing.go
* Slicing arrays
.play migrating-from-python/array-slicing.go
You can slice on an array, but it returns a slice
* Maps vs. Dicts and Sets
- Can't use `==` to compare maps
- Sets in Go are just implemented as maps with empty values:
.play migrating-from-python/set.go /START OMIT/,/END OMIT/
* Duck typing
.play migrating-from-python/duck.py
* Structural typing using interfaces
.play migrating-from-python/interfaces.go /START OMIT/,/END OMIT/
* Errors
What about Python's exception subtypes? (e.g., AttributeError, IOError, etc).
.play migrating-from-python/error-handling.go /START OMIT/,/END OMIT/
* Calling C code from Go
.code migrating-from-python/cgo.go
* Code organization
.code migrating-from-python/code-organization.txt
.code migrating-from-python/code-organization.go
.code migrating-from-python/code-organization-2.txt
* Code organization
.code migrating-from-python/code-organization-3.txt
* Packages
- Multiple files can belong to a package
- One package per directory
- The path to that directory determines the package's import path
- Build system locations dependencies from the source alone
* go get
Similar to pip install...
.code migrating-from-python/go-get.txt
.code migrating-from-python/go-get-2.txt
* go get example
* gofmt (instead of PEP 8)
Before:
.play migrating-from-python/before-gofmt.go
After:
.play migrating-from-python/after-gofmt.go
* PEP 257
.code migrating-from-python/doc-comment.go
[[http://godoc.org/net/url#User]]
* godoc example
* Unit testing
.code migrating-from-python/unit-testing.txt
.code migrating-from-python/unit-testing-coverage.txt
* Code coverage
.code migrating-from-python/coverprofile.txt
.code migrating-from-python/cover.txt
.image migrating-from-python/img/coverage.png
* Unit testing example
* Other tools
- go build -race (race detection)
- go vet (static analysis)
- go test -bench
- cpuprofile
- memprofile
* What about my interactive interpreter?
- Go compiles really quickly
- [[http://play.golang.org]]
- Unit tests are really easy (and fast)
- I still use the Python interactive interpreter
* Things I miss from Python
- coding speed
But...
* Learning more
[[http://tour.golang.org]]: Go language tour with exercises
[[http://exercism.io]]: Crowd-sourced mentorship
[[http://github.com/bgmerrell/presentations]]