-
Notifications
You must be signed in to change notification settings - Fork 1
/
10gosurprises.slide
310 lines (194 loc) · 6.65 KB
/
10gosurprises.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
10 Go Surprises
10 Nov 2014
Ken Walters
VP Engineering, Brad's Deals
https://github.com/lostghost/go_talks
@lostghost
* Let's sleep
.play -edit 10gosurprises/sleep.go /^func main/,/^}/
Does what we expect. Announces that it will sleep, sleeps for 500 miliseconds, awakes, and starts again.
Let's move our sleep delay into a named constant so that it's easier to maintain.
* Let's sleep
.play -edit 10gosurprises/sleepconst.go /Start/,/End/
Still does what we expect. Not very exciting.
What if we want random sleep intervals. Now we will have to use a var for Delay.
* Let's sleep
.play -edit 10gosurprises/sleepvar.go /Start/,/End/
Uh oh, compile error.
Can't multiply different types -- int for Delay and time.Duration for time.Millisecond.
That makes sense, but why did the previous two examples work?
* #1 - Untyped constants are untyped
* Untyped constants
.link http://blog.golang.org/constants
const Delay = 500
- Both the constant number 500 and the named contant "Delay" are untyped.
- Constants hold a default type, so if we infer the type from a constant, the inferred type will be the default type of the constant.
var Foo = Delay
- Foo will be the default type of Delay
var Bar float32 = Delay
- Bar will be type float32
* #2 - Iota
* Iota for Elegant Constants
.link https://blog.splice.com/iota-elegant-constants-golang/
- Iota keyword represents successive untyped integer constants.
- Reset whenever "const" appears.
- Increments on each line in the const block.
* Simple Example:
const (
C0 = iota // 0
C1 // 1
C2 // 2
_
_
C5 // 5
)
const (
C0, N0 = iota, iota // 0, 0
C1, N1 // 1, 1
C2, N2 // 2, 2
_
_
C5, N5 // 5, 5
)
* Simple Example:
type category int
const (
CatApparel category = iota // 0
CatBooks // 1
CatHome // 2
CatElectronics // 3
)
* Bitmask example:
type categories int
const (
CatApparel categories = 1 << iota // 1 << 0, 00000001
CatBooks // 1 << 1, 00000010
CatHome // 1 << 2, 00000100
CatElectronics // 1 << 3, 00001000
)
fmt.Println(CatBooks | CatElectronics)
// result: 00001010, 10 in decimal
* #3 - Type assertion to new interface with check
* Example
Assume that we are using a io.writer. We may have runtime configuration to change to the specific writer (http response, file, stdout, gzip compressor, etc.)
All of these satisfy the io.Writer interface, but some are buffered, and at times it may be important to ensure content is flushed.
The difference, in most cases, between a buffered writter and an unbuffered writer is the presence of a Flush method, but if we are coding to our standard io.Writer interface, then we can't call Flush.
* Unbuffered Writer
.play -edit 10gosurprises/writer.go /Start/,/End/
* Buffered Writer
.play -edit 10gosurprises/bufferedwriter.go /Start/,/End/
* Flush Our Buffered Writer
.play -edit 10gosurprises/flushwriter.go /Start/,/End/
* Flush Our Buffered Writer
.play -edit 10gosurprises/flushwritercheck.go /Start/,/End/
* Errors
f, err := os.Open(name)
if err != nil {
return err
}
codeUsing(f)
Creating Errors
yourError := errors.New("Something else blew up")
myError := fmt.Errorf("Something blew up")
* #4 - error is a builtin interface
* error interface
type error interface {
Error() string
}
- Packages implement their own error types.
- We can create custom error types with richer detail.
- We can use type assertion to check for error types and details.
foo, err := CustomFunction()
if customeErr, isCustomError := err.(CustomError); isCustomError {
if customErr.Code() == 42 {
// ... Recover gracefully
}
}
- *Always*stick*to*the*standard*error*interface*across*package*boundaries.*
* #5 - Panic and Recover
* Panic and Recover
.link http://blog.golang.org/defer-panic-and-recover
.play -edit 10gosurprises/panic.go /Start/,/End/
* Recover
.play -edit 10gosurprises/panicrecover.go /Start/,/End/
* #6 - Mock a filesystem
* Mock a filesystem
.link https://talks.golang.org/2012/10things.slide#8
.code 10gosurprises/filesystem.go /Start Interface/,/End Interface/
*
.code 10gosurprises/filesystem.go /Start osFS/,/End osFS/
.code 10gosurprises/filesystem.go /^func main/,/^}/
*
.code 10gosurprises/testfilesystem.go /Start/,/End/
* #7 - Stringer interface for custom types with fmt
* Stringer interface
- fmt package declares and tests for the Stringer inferface when formatting output.
type Stringer interface {
String() string
}
* Person
.play -edit 10gosurprises/printperson.go /Start/,/End/
* Person
.play -edit 10gosurprises/printprettyperson.go /Start/,/End/
* #8 - Custom JSON serialization
* Marshaler Interface
Just like the Stringer interface for the fmt package, the json package provides and tests for a Marshaler interface to allow us to create custom JSON serializers.
// Marshaler is the interface implemented by objects that
// can marshal themselves into valid JSON.
type Marshaler interface {
MarshalJSON() ([]byte, error)
}
*
.play -edit 10gosurprises/jsonperson.go /Start/,/End/
*
.play -edit 10gosurprises/jsonprettyperson.go /Start/,/End/
* #9 - More than just tests in tests
* Tests support three types of content
- Tests
- Benchmarks
- Examples
* Benchmarks
func BenchmarkAddStrings(b *testing.B) {
set := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Add(fmt.Sprintf("item_num_%d", i))
}
}
Run all benchmarks with:
go test -bench .
PASS
BenchmarkNew 20000000 133 ns/op
BenchmarkNewWithItems 2000000 840 ns/op
BenchmarkAddInts 5000000 312 ns/op
BenchmarkAddStrings 1000000 1093 ns/op
BenchmarkRemoveInts 10000000 214 ns/op
* Examples
func ExampleNew() {
// Create an empty set
emptySet := New()
log.Println(emptySet.IsEmpty()) // true
// Create a set with initial values
colors := New("red", "blue", "green")
log.Println(colors.Size()) // 3
// Create a set with initial values from a slice of interfaces
slice := []interface{}{1, 2, 3, 4, "one", "two", "three", "four"}
numbers := New(slice...)
log.Println(numbers.Size()) // 8
}
.link http://godoc.org/github.com/shopsmart/set
* #10 - Golang presentations with runable code
* Present
.link http://godoc.org/golang.org/x/tools/present
10gosurprises.slide
10 Go Surprises
10 Nov 2014
Ken Walters
VP Engineering, Brad's Deals
https://github.com/lostghost/go_talks
@lostghost
* Slide One
.play -edit 10gosurprises/sleep.go /^func main/,/^}/
.link https://github.com/lostghost/go_talks