Foreword |
|
xxi | |
Preface |
|
xxiii | |
Acknowledgments |
|
xxxi | |
About the Authors |
|
xxxv | |
|
1 Modules, Packages, and Dependencies |
|
|
1 | (26) |
|
|
1 | (3) |
|
|
2 | (1) |
|
|
3 | (1) |
|
|
3 | (1) |
|
|
4 | (2) |
|
|
5 | (1) |
|
Folders, Files, and Organization |
|
|
6 | (7) |
|
Multiple Packages in a Folder |
|
|
7 | (2) |
|
|
9 | (1) |
|
|
9 | (4) |
|
Importing Packages and Modules |
|
|
13 | (3) |
|
|
13 | (1) |
|
|
14 | (1) |
|
Resolving Import Name Clashes |
|
|
14 | (2) |
|
|
16 | (9) |
|
|
17 | (1) |
|
|
18 | (2) |
|
|
20 | (1) |
|
|
21 | (1) |
|
|
22 | (1) |
|
|
23 | (1) |
|
|
24 | (1) |
|
|
25 | (2) |
|
|
27 | (50) |
|
|
27 | (4) |
|
|
27 | (1) |
|
|
28 | (1) |
|
|
28 | (2) |
|
Keywords, Operators, and Delimiters |
|
|
30 | (1) |
|
|
31 | (4) |
|
Picking the Correct Numeric Type |
|
|
32 | (1) |
|
Overflow versus Wraparound |
|
|
32 | (2) |
|
|
34 | (1) |
|
|
35 | (2) |
|
Interpreted String Literals |
|
|
35 | (1) |
|
|
36 | (1) |
|
|
37 | (3) |
|
|
38 | (1) |
|
Iterating over UTF-8 Characters |
|
|
38 | (2) |
|
|
40 | (8) |
|
|
40 | (1) |
|
|
41 | (1) |
|
|
42 | (1) |
|
|
42 | (1) |
|
|
43 | (1) |
|
Variable Declaration and Initialization |
|
|
44 | (1) |
|
Assigning Multiple Values |
|
|
45 | (1) |
|
|
46 | (2) |
|
|
48 | (3) |
|
|
49 | (1) |
|
Untyped Constants (Inferred Typing) |
|
|
50 | (1) |
|
|
50 | (1) |
|
|
51 | (6) |
|
|
53 | (1) |
|
Conflicting with Package Names |
|
|
53 | (4) |
|
Exporting through Capitalization |
|
|
57 | (1) |
|
|
57 | (18) |
|
|
57 | (3) |
|
|
60 | (1) |
|
Multiple Arguments with Println |
|
|
61 | (1) |
|
Using Formatting Functions |
|
|
61 | (1) |
|
|
61 | (2) |
|
|
63 | (1) |
|
|
63 | (3) |
|
|
66 | (1) |
|
|
67 | (1) |
|
|
67 | (1) |
|
Printing a Value with More Detail |
|
|
68 | (1) |
|
Printing a Value's Go-Syntax Representation |
|
|
69 | (1) |
|
Using Formatting Verbs Incorrectly |
|
|
70 | (1) |
|
Explicit Argument Indexes |
|
|
71 | (1) |
|
Converting Strings to and from Numbers |
|
|
72 | (3) |
|
|
75 | (2) |
|
3 Arrays, Slices, and Iteration |
|
|
77 | (34) |
|
List Types: Arrays and Slices |
|
|
77 | (13) |
|
Differences between Arrays and Slices |
|
|
77 | (2) |
|
|
79 | (1) |
|
Initializing Arrays and Slices |
|
|
79 | (2) |
|
Array and Slice Zero Values |
|
|
81 | (1) |
|
Indexing Arrays and Slices |
|
|
81 | (1) |
|
|
82 | (1) |
|
Array and Slice Type Definitions |
|
|
83 | (2) |
|
Setting Array and Slice Values |
|
|
85 | (1) |
|
|
85 | (2) |
|
Appending Slices to Slices |
|
|
87 | (3) |
|
|
90 | (14) |
|
|
91 | (1) |
|
|
92 | (3) |
|
|
95 | (1) |
|
Make with Length and Capacity |
|
|
96 | (1) |
|
|
97 | (1) |
|
What Happens When a Slice Grows |
|
|
97 | (2) |
|
|
99 | (1) |
|
|
100 | (1) |
|
|
101 | (1) |
|
Converting an Array to a Slice |
|
|
102 | (2) |
|
|
104 | (6) |
|
|
104 | (1) |
|
Iterating over Arrays and Slices |
|
|
105 | (1) |
|
|
105 | (2) |
|
|
107 | (1) |
|
|
108 | (2) |
|
|
110 | (1) |
|
4 Maps and Control Structures |
|
|
111 | (30) |
|
|
111 | (18) |
|
|
112 | (1) |
|
|
113 | (1) |
|
|
114 | (1) |
|
|
114 | (1) |
|
|
115 | (1) |
|
|
116 | (2) |
|
|
118 | (2) |
|
|
120 | (1) |
|
Checking Map Key Existence |
|
|
120 | (1) |
|
|
121 | (1) |
|
|
122 | (1) |
|
|
123 | (1) |
|
|
124 | (2) |
|
|
126 | (3) |
|
|
129 | (6) |
|
|
130 | (1) |
|
|
131 | (1) |
|
|
132 | (2) |
|
|
134 | (1) |
|
|
135 | (5) |
|
|
138 | (1) |
|
|
139 | (1) |
|
|
140 | (1) |
|
|
141 | (26) |
|
|
141 | (10) |
|
|
141 | (1) |
|
Arguments of the Same Type |
|
|
142 | (1) |
|
|
143 | (1) |
|
Multiple Return Arguments |
|
|
144 | (1) |
|
|
145 | (2) |
|
|
147 | (1) |
|
|
148 | (1) |
|
|
149 | (1) |
|
|
150 | (1) |
|
Functions Accepting Arguments from Functions |
|
|
151 | (1) |
|
|
151 | (5) |
|
Variadic Argument Position |
|
|
152 | (1) |
|
|
153 | (1) |
|
When to Use a Variadic Argument |
|
|
154 | (2) |
|
|
156 | (6) |
|
Deferring with Multiple Returns |
|
|
156 | (1) |
|
|
157 | (1) |
|
|
158 | (1) |
|
|
158 | (1) |
|
Defer and Anonymous Functions |
|
|
159 | (1) |
|
|
160 | (2) |
|
|
162 | (4) |
|
|
163 | (1) |
|
|
164 | (1) |
|
Using init Functions for Side Effects |
|
|
164 | (2) |
|
|
166 | (1) |
|
6 Structs, Methods, and Pointers |
|
|
167 | (28) |
|
|
167 | (9) |
|
Declaring New Types in Go |
|
|
167 | (1) |
|
|
168 | (1) |
|
|
169 | (1) |
|
Initialization without Field Names |
|
|
170 | (2) |
|
|
172 | (1) |
|
|
173 | (1) |
|
|
173 | (2) |
|
|
175 | (1) |
|
|
176 | (6) |
|
Differences Between Methods and Functions |
|
|
177 | (1) |
|
|
177 | (1) |
|
Methods on Third-Party Types |
|
|
178 | (2) |
|
|
180 | (1) |
|
|
181 | (1) |
|
|
181 | (1) |
|
|
182 | (9) |
|
|
183 | (1) |
|
|
184 | (1) |
|
|
185 | (1) |
|
|
186 | (1) |
|
|
186 | (2) |
|
Value versus Pointer Receivers |
|
|
188 | (1) |
|
|
189 | (1) |
|
|
190 | (1) |
|
|
191 | (2) |
|
|
192 | (1) |
|
|
193 | (2) |
|
|
195 | (36) |
|
|
195 | (7) |
|
|
195 | (1) |
|
|
196 | (2) |
|
|
198 | (1) |
|
|
198 | (2) |
|
Usingt.Fatal (Recommended) |
|
|
200 | (1) |
|
Crafting Good Test Failure Messages |
|
|
201 | (1) |
|
|
202 | (4) |
|
|
202 | (1) |
|
Generating a Coverage Profile |
|
|
203 | (1) |
|
The go tool cover Command |
|
|
204 | (1) |
|
Generating an HTML Coverage Report |
|
|
205 | (1) |
|
|
206 | (1) |
|
|
206 | (7) |
|
Anatomy of a Table Driven Test |
|
|
207 | (1) |
|
Writing a Table Driven Test |
|
|
208 | (3) |
|
|
211 | (1) |
|
|
211 | (1) |
|
|
212 | (1) |
|
|
213 | (9) |
|
|
213 | (1) |
|
Running Tests with Subpackages |
|
|
213 | (1) |
|
|
214 | (1) |
|
|
215 | (1) |
|
|
216 | (1) |
|
Running Package Tests in Parallel |
|
|
217 | (1) |
|
Running Tests in Parallel |
|
|
217 | (1) |
|
|
218 | (1) |
|
|
219 | (1) |
|
|
220 | (1) |
|
|
221 | (1) |
|
|
222 | (7) |
|
|
222 | (4) |
|
Marking a Function as a Helper |
|
|
226 | (1) |
|
|
227 | (2) |
|
|
229 | (2) |
|
|
231 | (28) |
|
Concrete Types versus Interfaces |
|
|
231 | (2) |
|
Explicit Interface Implementation |
|
|
233 | (1) |
|
Implicit Interface Implementation |
|
|
234 | (1) |
|
|
235 | (2) |
|
|
237 | (2) |
|
|
239 | (2) |
|
|
241 | (1) |
|
Asserting Interface Implementation |
|
|
241 | (1) |
|
|
242 | (1) |
|
|
242 | (1) |
|
The Problem with Empty Interfaces |
|
|
243 | (1) |
|
|
243 | (1) |
|
|
243 | (6) |
|
Defining a Model Interface |
|
|
244 | (3) |
|
Implementing the Interface |
|
|
247 | (2) |
|
|
249 | (1) |
|
Defining an Validatable Interface |
|
|
250 | (1) |
|
|
250 | (2) |
|
|
251 | (1) |
|
|
252 | (1) |
|
Assertions through Switch |
|
|
252 | (3) |
|
|
253 | (1) |
|
|
254 | (1) |
|
|
255 | (2) |
|
Defining the Callback Interfaces |
|
|
255 | (1) |
|
|
256 | (1) |
|
|
257 | (2) |
|
|
259 | (52) |
|
|
259 | (5) |
|
|
261 | (1) |
|
|
262 | (1) |
|
|
263 | (1) |
|
|
264 | (9) |
|
|
265 | (1) |
|
|
265 | (4) |
|
Capturing and Returning Panic Values |
|
|
269 | (4) |
|
|
273 | (16) |
|
|
274 | (1) |
|
|
275 | (3) |
|
|
278 | (2) |
|
|
280 | (3) |
|
|
283 | (1) |
|
|
284 | (3) |
|
|
287 | (2) |
|
|
289 | (5) |
|
|
289 | (2) |
|
|
291 | (3) |
|
Wrapping and Unwrapping Errors |
|
|
294 | (7) |
|
|
296 | (1) |
|
|
297 | (1) |
|
|
298 | (3) |
|
|
301 | (1) |
|
|
301 | (6) |
|
|
302 | (2) |
|
|
304 | (3) |
|
|
307 | (2) |
|
|
309 | (2) |
|
|
311 | (24) |
|
|
311 | (23) |
|
The Problem with Interfaces |
|
|
311 | (4) |
|
|
315 | (2) |
|
|
317 | (3) |
|
Instantiating Generic Functions |
|
|
320 | (1) |
|
|
321 | (1) |
|
Multiple Type Constraints |
|
|
322 | (2) |
|
Underlying Type Constraints |
|
|
324 | (2) |
|
|
326 | (3) |
|
|
329 | (2) |
|
Mixing Method and Type Constraints |
|
|
331 | (1) |
|
|
332 | (2) |
|
|
334 | (1) |
|
|
335 | (38) |
|
Concurrency and Parallelism |
|
|
335 | (3) |
|
Concurrency Is Not Parallelism |
|
|
336 | (1) |
|
Understanding Concurrency |
|
|
336 | (2) |
|
|
338 | (4) |
|
|
338 | (1) |
|
|
339 | (1) |
|
|
339 | (1) |
|
Work Sharing and Stealing |
|
|
339 | (2) |
|
Don't Worry about the Scheduler |
|
|
341 | (1) |
|
|
342 | (1) |
|
Communicating with Channels |
|
|
342 | (9) |
|
|
343 | (1) |
|
Understanding Channel Blocking/Unblocking |
|
|
343 | (1) |
|
|
343 | (1) |
|
Sending and Receiving Values |
|
|
344 | (1) |
|
|
345 | (3) |
|
|
348 | (1) |
|
Listening to Channels with select |
|
|
348 | (1) |
|
|
349 | (1) |
|
Channels Are Not Message Queues |
|
|
349 | (2) |
|
|
351 | (1) |
|
Understanding Unidirectional Channels |
|
|
352 | (1) |
|
|
352 | (6) |
|
Detecting Closed Channels on Read |
|
|
354 | (1) |
|
Zero Value on Closed Read |
|
|
355 | (2) |
|
Closing an Already-Closed Channel |
|
|
357 | (1) |
|
Writing to a Closed Channel |
|
|
358 | (1) |
|
|
358 | (5) |
|
Basic Buffered Channel Example |
|
|
359 | (3) |
|
Reading from Closed Buffered Channels |
|
|
362 | (1) |
|
Capturing System Signals with Channels |
|
|
363 | (8) |
|
|
363 | (2) |
|
Implementing Graceful Shutdown |
|
|
365 | (2) |
|
Listening for System Signals |
|
|
367 | (1) |
|
Listening for Shutdown Confirmation |
|
|
368 | (2) |
|
Timing Out a Nonresponsive Shutdown |
|
|
370 | (1) |
|
|
371 | (2) |
|
|
373 | (46) |
|
|
374 | (6) |
|
|
374 | (1) |
|
|
375 | (1) |
|
|
376 | (1) |
|
|
376 | (2) |
|
|
378 | (1) |
|
|
378 | (1) |
|
|
379 | (1) |
|
|
380 | (1) |
|
|
381 | (3) |
|
Understanding the Nodal Hierarchy |
|
|
381 | (1) |
|
Wrapping with Context Values |
|
|
382 | (1) |
|
Following the Context Nodes |
|
|
382 | (2) |
|
|
384 | (4) |
|
Understanding Context Values |
|
|
384 | (2) |
|
|
386 | (2) |
|
Problems with String Keys |
|
|
388 | (5) |
|
|
388 | (2) |
|
|
390 | (3) |
|
Securing Context Keys and Values |
|
|
393 | (3) |
|
Securing by Not Exporting |
|
|
394 | (2) |
|
Cancellation Propagation with Contexts |
|
|
396 | (9) |
|
Creating a Cancelable Context |
|
|
397 | (1) |
|
|
398 | (2) |
|
Listening for Cancellation Confirmation |
|
|
400 | (5) |
|
|
405 | (3) |
|
Canceling at a Specific Time |
|
|
405 | (2) |
|
Canceling after a Duration |
|
|
407 | (1) |
|
|
408 | (3) |
|
|
409 | (1) |
|
Context Deadline Exceeded Error |
|
|
410 | (1) |
|
Listening for System Signals with Context |
|
|
411 | (5) |
|
|
413 | (3) |
|
|
416 | (3) |
|
|
419 | (48) |
|
Waiting for Goroutines with a WaitGroup |
|
|
419 | (14) |
|
|
419 | (2) |
|
|
421 | (1) |
|
|
421 | (1) |
|
|
422 | (4) |
|
|
426 | (6) |
|
|
432 | (1) |
|
Error Management with Error Groups |
|
|
433 | (10) |
|
|
434 | (2) |
|
|
436 | (3) |
|
Listening for Error Group Cancellation |
|
|
439 | (1) |
|
|
440 | (3) |
|
|
443 | (4) |
|
|
445 | (1) |
|
|
446 | (1) |
|
Wrapping Up the Race Detector |
|
|
447 | (1) |
|
Synchronizing Access with a Mutex |
|
|
447 | (12) |
|
|
449 | (2) |
|
|
451 | (2) |
|
|
453 | (2) |
|
|
455 | (4) |
|
Wrapping Up Read/Write Mutexes |
|
|
459 | (1) |
|
Performing Tasks Only Once |
|
|
459 | (7) |
|
|
460 | (2) |
|
|
462 | (1) |
|
Closing Channels with Once |
|
|
463 | (3) |
|
|
466 | (1) |
|
|
467 | (56) |
|
Directory Entries and File Information |
|
|
467 | (6) |
|
|
468 | (3) |
|
|
471 | (1) |
|
|
472 | (1) |
|
|
473 | (4) |
|
Skipping Directories and Files |
|
|
477 | (4) |
|
|
478 | (3) |
|
Creating Directories and Subdirectories |
|
|
481 | (3) |
|
|
484 | (5) |
|
Getting a File's Extension |
|
|
485 | (1) |
|
Getting a File's Directory |
|
|
485 | (1) |
|
Getting a File/Directory's Name |
|
|
486 | (1) |
|
|
486 | (2) |
|
|
488 | (1) |
|
Creating Multiple Directories |
|
|
489 | (3) |
|
|
492 | (5) |
|
|
495 | (2) |
|
|
497 | (3) |
|
|
498 | (2) |
|
|
500 | (3) |
|
|
503 | (2) |
|
|
505 | (1) |
|
|
506 | (4) |
|
|
508 | (1) |
|
|
509 | (1) |
|
|
510 | (3) |
|
|
512 | (1) |
|
|
513 | (4) |
|
|
515 | (2) |
|
|
517 | (5) |
|
|
518 | (2) |
|
Embedded Files in a Binary |
|
|
520 | (1) |
|
|
521 | (1) |
|
Embedding Files as a String or Byte Slice |
|
|
522 | (1) |
|
|
522 | (1) |
Index |
|
523 | |