Cover....1
Table of Contents....6
Preface....10
Cooking with Go....10
Who Are You?....10
Reading This Book....11
A Foolish Consistency Is the Hobgoblin of Little Minds....11
Wait! There’s More! (online)....11
1. Reading and Writing (I/O)....12
Recipe 1. Using In-Memory Readers and Writers to Support []byte....12
Recipe 2. Compressing Old Log Files....14
Recipe 3. Using bytes.Buffer to Generate SQL....18
Recipe 4. Conditionally Decompressing Files....19
Recipe 5. Implementing io.Writer for Frequency Calculation....22
Recipe 6. Using os.Pipe for Dynamic Data Generation....24
Recipe 7. Searching in a Memory Mapped File....27
Final Thoughts....30
2. Serializing Data....31
Recipe 8. Streaming Events with encoding/gob....31
Recipe 9. Parsing Complex JSON Documents....34
Recipe 10. Streaming JSON....36
Recipe 11. Handling Missing Values in JSON....38
Recipe 12. Serializing Custom Types....40
Recipe 13. Unmarshaling Dynamic Types....42
Recipe 14. Parsing Struct Tags....44
Final Thoughts....46
3. Utilizing HTTP....47
Recipe 15. Making GET Requests....47
Recipe 16. Streaming POST Requests....51
Recipe 17. Writing Middleware to Monitor Performance....53
Recipe 18. Setting Server Timeouts....54
Recipe 19. Supporting Several API Versions in the Same HTTP Server....55
Final Thoughts....57
4. Working with Text....58
Recipe 20. Using Formatting Verbs for Better Output....58
Recipe 21. Adding String Representation to Your Types....61
Recipe 22. Detecting Encoding....63
Recipe 23. Using Regular Expressions to Convert camelCase to lower_with_underscore....65
Recipe 24. Folding Strings for Case-Insensitive Comparison....67
Recipe 25. Using Unicode Normalization for Comparison....69
Final Thoughts....72
5. Working with Functions....73
Recipe 26. Using a Function Registry....73
Recipe 27. Using Functions as Options....78
Recipe 28. Using Closures to Provide Options with Arguments....80
Recipe 29. Passing Notifications with Functions....82
Recipe 30. Accessing Unexported Functions....84
Final Thoughts....87
6. Working with Basic Types....88
Recipe 31. Using the comma, ok Paradigm....89
Recipe 32. Using a Slice to Implement a Stack....92
Recipe 33. Calculating Cumulative Sum....96
Recipe 34. Serializing Time to/from JSON....100
Recipe 35. Using Composite Keys in Maps....103
Recipe 36. Parsing Time Strings....106
Final Thoughts....110
7. Working with Structs, Methods, and Interfaces....112
Recipe 37. Using Ad Hoc Interfaces....112
Recipe 38. Wrapping the http.ResponseWriter Interface....116
Recipe 39. Using Generics to Reduce Code Size....118
Recipe 40. Using Generics for Type-Safe Data Structures....120
Recipe 41. Using Generics for Better Type Safety....123
Final Thoughts....124
8. Working with Errors....125
Recipe 42. Handling and Returning Errors....125
Recipe 43. Handling Panics....127
Recipe 44. Handling Panics in Goroutines....129
Recipe 45. Checking Errors....132
Recipe 46. Wrapping Errors....134
Final Thoughts....136
9. Using Goroutines, Channels, and Context for Concurrency....138
Recipe 47. Converting Sequential Code to Parallel....139
Recipe 48. Limiting the Number of Goroutines with a Buffered Channel....142
Recipe 49. Using a Worker Pool with Channels....145
Recipe 50. Using context.Context for Timeouts....148
Recipe 51. Passing Logger with Request ID in Context....152
Final Thoughts....155
10. Lower-Level Concurrency....156
Recipe 52. Writing Idempotent Functions with sync.Once....156
Recipe 53. Waiting for Job Completion with sync.WaitGroup....158
Recipe 54. Allowing Multiple Readers with sync.RWMutex....161
Recipe 55. Using the Race Detector....162
Recipe 56. Using sync/atomic for a Faster Now....167
Final Thoughts....169
11. Working with Sockets....170
Recipe 57. Accepting Files over TCP Sockets....170
Recipe 58. Sending Files over TCP Sockets....173
Recipe 59. Writing a Serverless Platform....175
Recipe 60. Reading Time with NTP over UDP....178
Final Thoughts....181
12. Communicating with Non-Go Code....183
Recipe 61. Using os/exec to Ping Servers....184
Recipe 62. Calling C Functions to AlignText....187
Recipe 63. Redirecting Subprocess stdin and stdout to Prototype a Calculator....189
Recipe 64. Stemming Words Using a C Library....192
Final Thoughts....195
13. Testing Your Code....197
Recipe 65. Conditionally Running Continuous Integration Tests....197
Recipe 66. Reading Test Cases from a File....199
Recipe 67. Fuzzing Bugs Away....203
Recipe 68. Mocking HTTP Client Calls....206
Recipe 69. Writing Global Setup/Teardown Functions....209
Recipe 70. Running Services in Testing....211
Recipe 71. Writing a Linter....214
Final Thoughts....217
14. Building Applications....218
Recipe 72. Embedding Assets in Your Binary....218
Recipe 73. Injecting Version to Your Executable....220
Recipe 74. Ensuring Static Builds....223
Recipe 75. Using Build Tags for Conditional Builds....226
Recipe 76. Building Executables for Different Platforms....227
Recipe 77. Generating Code....230
Final Thoughts....232
15. Shipping Your Code....234
Recipe 78. Configuring Your Application....234
Recipe 79. Patching Dependencies....237
Recipe 80. Packaging Applications in Docker....240
Recipe 81. Catching Signals for Graceful Shutdown....242
Recipe 82. Writing Logs....244
Recipe 83. Using Metrics as Eyes to Production....246
Recipe 84. Debugging Running Services....249
Final Thoughts....252
Index....253
– SYMBOLS –....253
– A –....253
– B –....253
– C –....254
– D –....255
– E –....255
– F –....256
– G –....256
– H –....257
– I –....257
– J –....257
– K –....257
– L –....257
– M –....258
– N –....258
– O –....258
– P –....259
– Q –....259
– R –....259
– S –....260
– T –....261
– U –....261
– V –....262
– W –....262
– X –....262
– Y –....262
– Z –....262
Programmers love Go because it is lightweight, easy to work with, and easy to read. Go gives you the benefits of dynamically typed languages (speed of development) while keeping the upsides of strongly typed languages (security and performance).
Go is a simple language, but programming in Go is about more than just mastering syntax. There's an art to using Go effectively. Squeeze out the full use of advanced networking and multi-core power for which Go was designed. Save precious coding hours with recipes that help you manage objects, collect garbage, and safely use memory. Tackle Unicode, concurrency, and serialization with ease.
All the clean, reusable solutions you need for a wide variety of problems common to Go development. Outfitted with these recipes, your next apps will be more polished and more maintainable than ever.
Start out by tackling time and see how the Go time packager provides types that will do most of the heavy lifting for you. Next, work on recipes tailored to the nuances of processing text, like normalizing strings to avoid bugs. From there, whip up some functions on-the-fly and store functions in variables and data structures.
Ever wondered why Go seems to be peppered with error handling? Working through the next recipes, you'll discover the benefits, which include more robust code. In the section on HTTP, you'll learn tricks like running multiple HTTP servers on the same port and setting timeouts. With concurrency recipes, you'll limit the number of goroutines to improve performance, give your code awareness of timeouts, and decide when to keep your code sequential instead of making it concurrent.
Throughout the book, you'll make Go sizzle following simple recipes and tweaking them to fit your own apps. Using tools like strong typing and concurrency primitives, build a Go codebase that stays maintainable at scale.
You should know the Go language syntax and have some experience in programming. You will need a Go SDK, a Git client, and for some of the chapters, a C compiler.