100 Go Mistakes....1
contents....8
preface....14
acknowledgments....15
about this book....16
Who should read this book....16
How this book is organized: A roadmap....16
About the code....17
liveBook discussion forum....18
about the author....19
about the cover illustration....20
1 Go: Simple to learn but hard to master....21
1.1 Go outline....22
1.2 Simple doesn’t mean easy....23
1.3 100 Go mistakes....24
1.3.1 Bugs....24
1.3.2 Needless complexity....25
1.3.3 Weaker readability....25
1.3.4 Suboptimal or unidiomatic organization....25
1.3.5 Lack of API convenience....26
1.3.6 Under-optimized code....26
1.3.7 Lack of productivity....26
Summary....26
2 Code and project organization....27
2.1 #1: Unintended variable shadowing....27
2.2 #2: Unnecessary nested code....29
2.3 #3: Misusing init functions....32
2.3.1 Concepts....32
2.3.2 When to use init functions....35
2.4 #4: Overusing getters and setters....37
2.5 #5: Interface pollution....38
2.5.1 Concepts....38
2.5.2 When to use interfaces....41
2.5.3 Interface pollution....44
2.6 #6: Interface on the producer side....45
2.7 #7: Returning interfaces....47
2.8 #8: any says nothing....48
2.9 #9: Being confused about when to use generics....51
2.9.1 Concepts....51
2.9.2 Common uses and misuses....54
2.10 #10: Not being aware of the possible problems with type embedding....56
2.11 #11: Not using the functional options pattern....60
2.11.1 Config struct....61
2.11.2 Builder pattern....62
2.11.3 Functional options pattern....64
2.12 #12: Project misorganization....66
2.12.1 Project structure....66
2.12.2 Package organization....67
2.13 #13: Creating utility packages....68
2.14 #14: Ignoring package name collisions....70
2.15 #15: Missing code documentation....71
2.16 #16: Not using linters....73
Summary....74
3 Data types....76
3.1 #17: Creating confusion with octal literals....76
3.2 #18: Neglecting integer overflows....77
3.2.1 Concepts....78
3.2.2 Detecting integer overflow when incrementing....79
3.2.3 Detecting integer overflows during addition....80
3.2.4 Detecting an integer overflow during multiplication....80
3.3 #19: Not understanding floating points....81
3.4 #20: Not understanding slice length and capacity....84
3.5 #21: Inefficient slice initialization....88
3.6 #22: Being confused about nil vs. empty slices....91
3.7 #23: Not properly checking if a slice is empty....94
3.8 #24: Not making slice copies correctly....96
3.9 #25: Unexpected side effects using slice append....97
3.10 #26: Slices and memory leaks....100
3.10.1 Leaking capacity....100
3.10.2 Slice and pointers....101
3.11 #27: Inefficient map initialization....104
3.11.1 Concepts....104
3.11.2 Initialization....106
3.12 #28: Maps and memory leaks....107
3.13 #29: Comparing values incorrectly....110
Summary....113
4 Control structures....115
4.1 #30: Ignoring the fact that elements are copied in range loops....115
4.1.1 Concepts....116
4.1.2 Value copy....116
4.2 #31: Ignoring how arguments are evaluated in range loops....118
4.2.1 Channels....120
4.2.2 Array....120
4.3 #32: Ignoring the impact of using pointer elements in range loops....122
4.4 #33: Making wrong assumptions during map iterations....125
4.4.1 Ordering....125
4.4.2 Map insert during iteration....127
4.5 #34: Ignoring how the break statement works....128
4.6 #35: Using defer inside a loop....130
Summary....132
5 Strings....133
5.1 #36: Not understanding the concept of a rune....134
5.2 #37: Inaccurate string iteration....135
5.3 #38: Misusing trim functions....138
5.4 #39: Under-optimized string concatenation....139
5.5 #40: Useless string conversions....141
5.6 #41: Substrings and memory leaks....143
Summary....145
6 Functions and methods....146
6.1 #42: Not knowing which type of receiver to use....147
6.2 #43: Never using named result parameters....149
6.3 #44: Unintended side effects with named result parameters....152
6.4 #45: Returning a nil receiver....153
6.5 #46: Using a filename as a function input....156
6.6 #47: Ignoring how defer arguments and receivers are evaluated....158
6.6.1 Argument evaluation....158
6.6.2 Pointer and value receivers....161
Summary....162
7 Error management....163
7.1 #48: Panicking....163
7.2 #49: Ignoring when to wrap an error....166
7.3 #50: Checking an error type inaccurately....169
7.4 #51: Checking an error value inaccurately....172
7.5 #52: Handling an error twice....174
7.6 #53: Not handling an error....176
7.7 #54: Not handling defer errors....178
Summary....180
8 Concurrency: Foundations....182
8.1 #55: Mixing up concurrency and parallelism....183
8.2 #56: Thinking concurrency is always faster....186
8.2.1 Go scheduling....186
8.2.2 Parallel merge sort....189
8.3 #57: Being puzzled about when to use channels or mutexes....193
8.4 #58: Not understanding race problems....194
8.4.1 Data races vs. race conditions....194
8.4.2 The Go memory model....199
8.5 #59: Not understanding the concurrency impacts of a workload type....201
8.6 #60: Misunderstanding Go contexts....206
8.6.1 Deadline....206
8.6.2 Cancellation signals....207
8.6.3 Context values....208
8.6.4 Catching a context cancellation....210
Summary....212
9 Concurrency: Practice....213
9.1 #61: Propagating an inappropriate context....213
9.2 #62: Starting a goroutine without knowing when to stop it....216
9.3 #63: Not being careful with goroutines and loop variables....218
9.4 #64: Expecting deterministic behavior using select and channels....220
9.5 #65: Not using notification channels....224
9.6 #66: Not using nil channels....225
9.7 #67: Being puzzled about channel size....231
9.8 #68: Forgetting about possible side effects with string formatting....233
9.8.1 etcd data race....233
9.8.2 Deadlock....234
9.9 #69: Creating data races with append....236
9.10 #70: Using mutexes inaccurately with slices and maps....238
9.11 #71: Misusing sync.WaitGroup....241
9.12 #72: Forgetting about sync.Cond....243
9.13 #73: Not using errgroup....248
9.14 #74: Copying a sync type....251
Summary....253
10 The standard library....254
10.1 #75: Providing a wrong time duration....255
10.2 #76: time.After and memory leaks....255
10.3 #77: Common JSON-handling mistakes....258
10.3.1 Unexpected behavior due to type embedding....258
10.3.2 JSON and the monotonic clock....260
10.3.3 Map of any....263
10.4 #78: Common SQL mistakes....264
10.4.1 Forgetting that sql.Open doesn’t necessarily establish connections to a database....264
10.4.2 Forgetting about connections pooling....265
10.4.3 Not using prepared statements....266
10.4.4 Mishandling null values....267
10.4.5 Not handling row iteration errors....268
10.5 #79: Not closing transient resources....269
10.5.1 HTTP body....270
10.5.2 sql.Rows....272
10.5.3 os.File....273
10.6 #80: Forgetting the return statement after replying to an HTTP request....275
10.7 #81: Using the default HTTP client and server....276
10.7.1 HTTP client....276
10.7.2 HTTP server....279
Summary....280
11 Testing....282
11.1 #82: Not categorizing tests....282
11.1.1 Build tags....283
11.1.2 Environment variables....284
11.1.3 Short mode....285
11.2 #83: Not enabling the -race flag....286
11.3 #84: Not using test execution modes....288
11.3.1 The parallel flag....289
11.3.2 The -shuffle flag....290
11.4 #85: Not using table-driven tests....291
11.5 #86: Sleeping in unit tests....294
11.6 #87: Not dealing with the time API efficiently....298
11.7 #88: Not using testing utility packages....301
11.7.1 The httptest package....301
11.7.2 The iotest package....303
11.8 #89: Writing inaccurate benchmarks....305
11.8.1 Not resetting or pausing the timer....306
11.8.2 Making wrong assumptions about micro-benchmarks....307
11.8.3 Not being careful about compiler optimizations....309
11.8.4 Being fooled by the observer effect....311
11.9 #90: Not exploring all the Go testing features....314
11.9.1 Code coverage....314
11.9.2 Testing from a different package....315
11.9.3 Utility functions....315
11.9.4 Setup and teardown....316
Summary....317
12 Optimizations....319
12.1 #91: Not understanding CPU caches....320
12.1.1 CPU architecture....320
12.1.2 Cache line....321
12.1.3 Slice of structs vs. struct of slices....324
12.1.4 Predictability....325
12.1.5 Cache placement policy....327
12.2 #92: Writing concurrent code that leads to false sharing....332
12.3 #93: Not taking into account instruction-level parallelism....335
12.4 #94: Not being aware of data alignment....341
12.5 #95: Not understanding stack vs. heap....344
12.5.1 Stack vs. heap....344
12.5.2 Escape analysis....349
12.6 #96: Not knowing how to reduce allocations....351
12.6.1 API changes....351
12.6.2 Compiler optimizations....352
12.6.3 sync.Pool....352
12.7 #97: Not relying on inlining....354
12.8 #98: Not using Go diagnostics tooling....357
12.8.1 Profiling....357
12.8.2 Execution tracer....364
12.9 #99: Not understanding how the GC works....367
12.9.1 Concepts....367
12.9.2 Examples....369
12.10 #100: Not understanding the impacts of running Go in Docker and Kubernetes....372
Summary....374
Final words....375
index....377
A....377
B....377
C....377
D....378
E....379
F....379
G....379
H....380
I....380
J....380
L....380
M....380
N....381
O....381
P....381
R....382
S....382
T....383
U....383
V....384
W....384
100 Go Mistakes - back....385
100 Go Mistakes and How to Avoid Them puts a spotlight on common errors in Go code you might not even know you’re making. You’ll explore key areas of the language such as concurrency, testing, data structures, and more—and learn how to avoid and fix mistakes in your own projects. As you go, you’ll navigate the tricky bits of handling JSON data and HTTP services, discover best practices for Go code organization, and learn how to use slices efficiently.
Understanding mistakes is the best way to improve the quality of your code. This unique book examines 100 bugs and inefficiencies common to Go applications, along with tips and techniques to avoid making them in your own projects.
100 Go Mistakes and How to Avoid Them shows you how to replace common programming problems in Go with idiomatic, expressive code. In it, you’ll explore dozens of interesting examples and case studies as you learn to spot mistakes that might appear in your own applications. Expert author Teiva Harsanyi organizes the error avoidance techniques into convenient categories, ranging from types and strings to concurrency and testing.
Spot errors in your Go code you didn’t even know you were making and boost your productivity by avoiding common mistakes and pitfalls.100 Go Mistakes and How to Avoid Them shows you how to: