Cover....1
Title Page....2
Copyright and Credits....3
Dedication....4
About Packt....5
Contributors....6
Table of Contents....8
Preface....16
Chapter 1: I/O and Filesystems....22
Technical requirements....23
Using the common I/O interfaces....23
How to do it.......24
How it works.......27
Using the bytes and strings packages....27
How to do it.......28
How it works.......32
Working with directories and files....32
How to do it.......32
How it works.......36
Working with the CSV format....37
How to do it.......37
How it works.......42
Working with temporary files....42
How to do it.......43
How it works.......45
Working with text/template and html/template....45
How to do it.......45
How it works.......52
Chapter 2: Command-Line Tools....53
Technical requirements....54
Using command-line flags....54
How to do it.......54
How it works.......58
Using command-line arguments....58
How to do it.......59
How it works.......62
Reading and setting environment variables....63
How to do it.......63
How it works.......67
Configuration using TOML, YAML, and JSON....67
How to do it.......68
How it works.......74
Working with Unix pipes....74
How to do it.......75
How it works.......77
Catching and handling signals....77
How to do it.......77
How it works.......80
An ANSI coloring application....80
How to do it.......80
How it works.......83
Chapter 3: Data Conversion and Composition....84
Technical requirements....84
Converting data types and interface casting....85
How to do it.......85
How it works.......89
Working with numeric data types using math and math/big....89
How to do it.......89
How it works.......93
Currency conversions and float64 considerations....93
How to do it.......93
How it works.......97
Using pointers and SQL NullTypes for encoding and decoding....97
How to do it.......98
How it works.......103
Encoding and decoding Go data....103
How to do it.......103
How it works.......107
Structure tags and basic reflection in Go....108
How to do it.......108
How it works.......113
Implementing collections via closures....114
How to do it.......114
How it works.......118
Chapter 4: Error Handling in Go....119
Technical requirements....119
Handling errors and the Error interface....120
How to do it.......120
How it works.......123
Using the pkg/errors package and wrapping errors....123
How to do it.......124
How it works.......127
Using the log package and understanding when to log errors....127
How to do it.......128
How it works.......130
Structured logging with the apex and logrus packages....131
How to do it.......131
How it works.......135
Logging with the context package....135
How to do it.......135
How it works.......138
Using package-level global variables....139
How to do it.......139
How it works.......142
Catching panics for long-running processes....142
How to do it.......142
How it works.......144
Chapter 5: Network Programming....145
Technical requirements....145
Writing a TCP/IP echo server and client....146
How to do it.......146
How it works.......149
Writing a UDP server and client....150
How to do it.......150
How it works.......154
Working with domain name resolution....154
How to do it.......155
How it works.......157
Working with WebSockets....157
How to do it.......158
How it works.......162
Working with net/rpc for calling remote methods....163
How to do it.......163
How it works.......166
Using net/mail for parsing emails....167
How to do it.......167
How it works.......169
Chapter 6: All about Databases and Storage....170
Using the database/sql package with MySQL....170
Getting ready....171
How to do it.......171
How it works.......175
Executing a database transaction interface....176
Getting ready....176
How to do it.......176
How it works.......180
Connection pooling, rate limiting, and timeouts for SQL....180
Getting ready....180
How to do it.......181
How it works.......183
Working with Redis....184
Getting ready....184
How to do it.......184
How it works.......188
Using NoSQL with MongoDB....188
Getting ready....189
How to do it.......189
How it works.......192
Creating storage interfaces for data portability....192
Getting ready....193
How to do it.......193
How it works.......197
Chapter 7: Web Clients and APIs....198
Technical requirements....198
Initializing, storing, and passing http.Client structures....199
How to do it.......199
How it works.......203
Writing a client for a REST API....203
How to do it.......204
How it works.......207
Executing parallel and async client requests....207
How to do it.......207
How it works.......210
Making use of OAuth2 clients....210
Getting ready....211
How to do it.......211
How it works.......214
Implementing an OAuth2 token storage interface....214
Getting ready....215
How to do it.......215
How it works.......220
Wrapping a client in added functionality and function composition....221
How to do it.......222
How it works.......225
Understanding GRPC clients....226
Getting ready....226
How to do it.......226
How it works.......230
Using twitchtv/twirp for RPC....230
Getting ready....231
How to do it.......231
How it works.......235
Chapter 8: Microservices for Applications in Go....236
Technical requirements....237
Working with web handlers, requests, and ResponseWriter instances....237
How to do it.......238
How it works.......241
Using structures and closures for stateful handlers....241
How to do it.......242
How it works.......246
Validating input for Go structures and user inputs....246
How to do it.......246
How it works.......250
Rendering and content negotiation....251
How to do it.......251
How it works.......254
Implementing and using middleware....254
How to do it.......255
How it works.......259
Building a reverse proxy application....259
How to do it.......259
How it works.......263
Exporting GRPC as a JSON API....263
Getting ready....263
How to do it.......264
How it works.......270
Chapter 9: Testing Go Code....271
Technical requirements....271
Mocking using the standard library....272
How to do it.......272
How it works.......276
Using the Mockgen package to mock interfaces....276
Getting ready....277
How to do it.......277
How it works.......280
Using table-driven tests to improve coverage....281
How to do it.......281
How it works.......283
Using third-party testing tools....284
Getting ready....284
How to do it.......284
How it works.......288
Behavior testing using Go....289
Getting ready....289
How to do it.......290
How it works.......293
Chapter 10: Parallelism and Concurrency....295
Technical requirements....296
Using channels and the select statement....296
How to do it.......296
How it works.......299
Performing async operations with sync.WaitGroup....300
How to do it.......300
How it works.......304
Using atomic operations and mutex....304
How to do it.......305
How it works.......308
Using the context package....309
How to do it.......309
How it works.......312
Executing state management for channels....312
How to do it.......313
How it works.......316
Using the worker pool design pattern....317
How to do it.......317
How it works.......322
Using workers to create pipelines....322
How to do it.......322
How it works.......327
Chapter 11: Distributed Systems....328
Technical requirements....328
Using service discovery with Consul....329
How to do it.......329
How it works.......332
Implementing basic consensus using Raft....333
How to do it.......333
How it works.......340
Using containerization with Docker....341
Getting ready....341
How to do it.......341
How it works.......345
Orchestration and deployment strategies....345
How to do it.......346
How it works.......349
Monitoring applications....349
How to do it.......349
How it works.......353
Collecting metrics....353
Getting ready....354
How to do it.......354
How it works.......357
Chapter 12: Reactive Programming and Data Streams....358
Technical requirements....358
Using Goflow for data flow programming....359
How to do it.......359
How it works.......362
Using Kafka with Sarama....363
Getting ready....363
How to do it.......363
How it works.......366
Using async producers with Kafka....366
Getting ready....367
How to do it.......367
How it works.......370
Connecting Kafka to Goflow....370
Getting ready....371
How to do it.......371
How it works.......374
Writing a GraphQL server in Go....375
How to do it.......375
How it works.......381
Chapter 13: Serverless Programming....382
Go programming on Lambda with Apex....382
Getting ready....383
How to do it.......383
How it works.......387
Apex serverless logging and metrics....387
Getting ready....388
How to do it.......388
How it works.......391
Google App Engine with Go....391
Getting ready....392
How to do it.......392
How it works.......396
Working with Firebase using firebase.google.com/go....397
Getting ready....397
How to do it.......398
How it works.......401
Chapter 14: Performance Improvements, Tips, and Tricks....402
Technical requirements....402
Using the pprof tool....403
How to do it.......403
How it works.......407
Benchmarking and finding bottlenecks....408
How to do it.......408
How it works.......412
Memory allocation and heap management....412
How to do it.......412
How it works.......415
Using fasthttprouter and fasthttp....416
How to do it.......416
How it works.......419
Other Books You May Enjoy....420
Index....423
Go (or Golang) is a statically typed programming language developed at Google. Known for its vast standard library, it also provides features such as garbage collection, type safety, dynamic-typing capabilities, and additional built-in types. This book will serve as a reference while implementing Go features to build your own applications.
This Go cookbook helps you put into practice the advanced concepts and libraries that Golang offers. The recipes in the book follow best practices such as documentation, testing, and vendoring with Go modules, as well as performing clean abstractions using interfaces. You'll learn how code works and the common pitfalls to watch out for. The book covers basic type and error handling, and then moves on to explore applications, such as websites, command-line tools, and filesystems, that interact with users. You'll even get to grips with parallelism, distributed systems, and performance tuning.
By the end of the book, you'll be able to use open source code and concepts in Go programming to build enterprise-class applications without any hassle.
If you're a web developer, programmer, or enterprise developer looking for quick solutions to common and not-so-common problems in Go programming, this book is for you. Basic knowledge of the Go language is assumed.