-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogramming-in-go.slide
351 lines (202 loc) · 8.99 KB
/
programming-in-go.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
Programming in Go
Brian G. Merrell
Software Engineer, Walmart eCommerce
bgmerrell (Twitter, Gmail, GitHub, Freenode, etc)
* Who am I?
- Running primarily Unix/Linux since S.u.S.E. (openSUSE) 5.1 (1997)
- First network: Linux, PS2, and OpenBSD router and firewall (33.6 kbps)
- First jobs: Dial-up ISP, Samba File Servers
- Novell (2006-2010): Linux Desktop (Accessibility, Google Chrome) (C, C++, C#, Java, Python)
- Fusion-io (2010-2014): Distributed and embedded systems (C, Python)
- Web backend era (2014-Present): Web backend systems development (Go)
* Goal of this talk
Understand Go well enough to reason about when and where to use it, and to facilitate becoming proficient.
.image programming-in-go/img/gopher.png
* History
- Appeared in 2009 (with 1.0 being released March 2012)
- Ken Thompson (Unix, B), Rob Pike (Plan 9, UTF8), Robert Griesemer (Java HotSpot VM)
- Wanted to build a language that is efficient, safe, and easy to use
- Doing research into programming language design was not the purpose
* Justification -- Why another language?
"Programming had become too difficult"
C++, Java: Too complex, hard to use, verbose
Compilation is too slow
The computing landscape and scale has changed
Many programmers were choosing ease (i.e., Perlythonubyscript) over safety and efficiency
* Go is...
Compiled (to machine code)
Strongly typed
Statically typed (with type inference)
Capable of statically verified duck typing (via interfaces)
Garbage collected
Pass-by-value
Similar to C in its syntax
Open Source (now on GitHub!)
* Go does not have...
Classes
Exceptions
Assertions
Templates / generics
Operator overloading
Function overloading
Optional function parameters
Macros
Implicit type conversions
Many abstractions you might be used to, e.g: a set type, an ordered map, generators, decorators, iterators, function to delete array indexes etc.
* Adoption
.image programming-in-go/img/redmonk-language-rankings.png 561 879
* Companies using Go
.image programming-in-go/img/companies-using-go-2015.jpg 561 977
* Where can I use Go in the technology stack?
.image programming-in-go/img/go-spectrum.gif 561 977
* Today's Language Landscape
- Following graphs taken from Brad Fitzpatrick's GoCon Tokyo talk: "Go: 90% Perfect, 100% of the time."
- Subjective
- There's always trade-offs to consider
* Fast & Fun
.image programming-in-go/img/funfast.svg
* Concurrency
.image programming-in-go/img/concurrency.svg
* What I dislike about Go
- Ungoogleable name (use golang)
- Inconsistencies for primitives, some keywords
- Despite being small language there are still too many ways to do the same thing
- Lack of generics can bring some awkwardness
- Array operations are often unintuitive
- Not suitable for (non-OS) embedded software
- Still a lot of immature libraries
- No big innovation to promote programming language research
- R̶u̶n̶t̶i̶m̶e̶ ̶i̶s̶ ̶s̶t̶i̶l̶l̶ ̶y̶o̶u̶n̶g̶ ̶(̶G̶C̶,̶ ̶g̶o̶r̶o̶u̶t̶i̶n̶e̶ ̶m̶a̶n̶a̶g̶e̶m̶e̶n̶t̶)̶
* Go Release Timeline
- 1.0, 2012
- 1.1, 2013
- 1.2, 2013
- 1.3, 2014
- 1.4, 2014
- 1.5, 2015
- 1.6, 2016
- 1.7, 2016
- 1.8, 2017
* Garbage Collection in Go 1.8+
- "Go's new garbage collector is a concurrent, tri-color, mark-sweep collector... where stop-the-world pauses are no longer a barrier to [Go]" [1]
- 100μs typical worst case stop-the-world (STW) pauses depending on heap size, often < 10μs
- Just keeps improving (seconds, <10ms, <100μs), just since 2015. Next: eliminate STW altogether
.image programming-in-go/img/go-gc.png
[1] https://blog.golang.org/go15gc
* Why I like Go
- I almost never actually care about the things I dislike
- Get stuff done (Go is a pragmatic language)
- Concurrency is a breeze
- Compile time checks (fast!), dynamic language feel
- Replaces code that I would have written in a dynamic language
- Fast (relatively, of course)
- Small language
- Easy to read (programming is communicating)
- Great tooling
- Cool community
* Learning a new language
"Go is a new language. Although it borrows ideas from existing languages, it has unusual properties that make effective Go programs different in character from programs written in its relatives. 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. On the other hand, thinking about the problem from a Go perspective could produce a successful but quite different program. In other words, to write Go well, it's important to understand its properties and idioms."
Effective Go ([[http://golang.org/doc/effective_go.html]])
* Hello World
.code programming-in-go/hello-world.go
* Basic types
.code programming-in-go/basic-types.txt
* Variable declarations
.play programming-in-go/variable-declarations.go /START OMIT/,/END OMIT/
* Looping, if/else, switch, const
.play programming-in-go/loop-switch-if.go
* Structs (defining your own types)
.play programming-in-go/structs.go
* Don't fear Go pointers
.play programming-in-go/pointers.go /START OMIT/,/END OMIT/
* Slices
.play programming-in-go/slices.go /START OMIT/,/END OMIT/
.play programming-in-go/range.go
* Maps
.play programming-in-go/maps.go
* Function values and function literals
.play programming-in-go/func-vals-and-func-literals.go
* Types with reference-like behavior
- Map
- Slice
- Channel
* Methods
.play programming-in-go/methods-pointers.go
* Pointer Receivers vs. Value Receivers
If in doubt, use a pointer receiver (it will behave like a method in other modern languages)
*Specifics*
- A pointer receiver should be used to persist object modifications
- A pointer receiver should be used for large objects
- A value receiver can be used for small objects (but the thing about persistence still applies)
For more information: [[https://github.com/golang/go/wiki/CodeReviewComments#receiver-type]]
* Methods on any type
.play programming-in-go/methods-on-any-type.go
* Polymorphism and Inheritance Review
- Polymorphism defined: "Ability of one type to stand in for many types"[1]
- Most statically-typed "object-oriented programming languages offer subtyping polymorphism using subclassing (also known as inheritance)."[2]
- Most dynamically-typed languages provide polymorphism via duck typing and inheritance is used for borrowing implementation
- Go provides polymorphism via interfaces (think statically verified duck typing)
- Go provides implementation borrowing via embedding
[1] Koenig, A. (2000). Accelerated C++.
[2] [[http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29]]
* Embedding
.play programming-in-go/embedding.go /START OMIT/,/END OMIT/
* Real World Embedding (go/src/net/hosts.go)
.code programming-in-go/embedding2.go
* Interfaces
.play programming-in-go/interfaces.go /START OMIT/,/END OMIT/
* Real World Interfaces (go/src/sort/sort.go)
.code programming-in-go/interfaces-sort.go
* Real World Interfaces (continued)
.play programming-in-go/interfaces-sort2.go /START OMIT/,/END OMIT/
* Empty interfaces (and variadic functions)
.code programming-in-go/fmt-println.go
* Errors
.play programming-in-go/errors-simple.go /START OMIT/,/END OMIT/
* Custom errors and type switching
.play programming-in-go/errors.go /START OMIT/,/END OMIT/
* Concurrency
.play programming-in-go/concurrency-simple.go /START OMIT/,/END OMIT/
* Channels
.play programming-in-go/channels.go /START OMIT/,/END OMIT/
* Buffered Channels
.play programming-in-go/buffered-channels.go
* Buffered Channels (continued)
.play programming-in-go/buffered-channels2.go /START OMIT/,/END OMIT/
* Select
.play programming-in-go/concurrency-for-with-select.go
* Real World Concurrency
.code programming-in-go/concurrency-for-with-select2.go
* Real World Concurrency (continued)
.code programming-in-go/concurrency-for-with-select3.go
* Web Server
.play programming-in-go/web-server.go
* Great tooling
gofmt
go get
go build [-race] ([[https://golang.org/doc/articles/race_detector.html]])
go test [-cover] ([[https://blog.golang.org/cover]])
go test [-bench] ([[http://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go]])
go vet
godoc
cpuprofile / memprofile ([[http://blog.golang.org/profiling-go-programs]])
* Common mistakes
- The closure mistake
.code programming-in-go/closure-mistake.go
- Not accepting interfaces (and underutilizing interfaces in general)
- Not protecting concurrent access to shared state (e.g., maps)
- Confusing embedding with inheritance
- Defaulting to 3rd party libraries (web frameworks, unit testing frameworks)
- ALL_CAPS constants
- Receiver type
* Next steps
[[http://tour.golang.org]]: Go language tour with exercises
[[https://golang.org/doc/install]]: Installing Go
[[https://golang.org/doc/code.html]]: How to Write Go Code
[[https://golang.org/doc/effective_go.html]]: Effective Go
[[http://utahgophers.com]]: Utah Gopher User Group
[[https://golang.org/ref/spec]]: Go Language Specification
[[https://github.com/golang/go]]: Go source
[[http://gophercon.com]]: Gophercon 2015 (July 7-10th)
*
.image programming-in-go/img/utah-gopher-announce-with-day.png 561 977