Table Of ContentGo in Practice
MATT BUTCHER
MATT FARINA
MANNING
SHELTER ISLAND
©2016 by Manning Publications Co.
ISBN 9781633430075
Printed in the United States of America
brief contents
PART 1 BACKGROUND AND FUNDAMENTALS ...............................1
1 ■ Getting into Go 3
2 ■ A solid foundation 27
3 ■ Concurrency in Go 59
PART 2 WELL-ROUNDED APPLICATIONS....................................85
4 ■ Handling errors and panic 87
5 ■ Debugging and testing 113
PART 3 AN INTERFACE FOR YOUR APPLICATIONS....................145
6 ■ HTML and email template patterns 147
7 ■ Serving and receiving assets and forms 168
8 ■ Working with web services 194
PART 4 TAKING YOUR APPLICATIONS TO THE CLOUD .............215
9 ■ Using the cloud 217
10 ■ Communication between cloud services 235
11 ■ Reflection and code generation 253
contents
foreword xiii
preface xv
acknowledgments xvi
about this book xviii
about the authors xx
about the cover illustration xxi
PART 1 BACKGROUND AND FUNDAMENTALS....................1
1 Getting into Go 3
1.1 What is Go? 4
1.2 Noteworthy aspects of Go 6
Multiple return values 6 ■ A modern standard library 7
Concurrency with goroutines and channels 9 ■ Go the toolchain—
more than a language 13
1.3 Go in the vast language landscape 17
C and Go 17 ■ Java and Go 18 ■ Python, PHP, and Go 19
JavaScript, Node.js, and Go 21
1.4 Getting up and running in Go 22
Installing Go 22 ■ Working with Git, Mercurial, and version
control 22 ■ Exploring the workspace 23 ■ Working with
environment variables 23
1.5 Hello, Go 24
1.6 Summary 25
2 A solid foundation 27
2.1 Working with CLI applications, the Go way 28
Command-line flags 28
TECHNIQUE 1 GNU/UNIX-style command-line arguments 31
Command-line frameworks 33
TECHNIQUE 2 Avoiding CLI boilerplate code 33
2.2 Handling configuration 38
TECHNIQUE 3 Using configuration files 39
TECHNIQUE 4 Configuration via environment variables 43
2.3 Working with real-world web servers 44
Starting up and shutting down a server 45
TECHNIQUE 5 Graceful shutdowns using manners 46
Routing web requests 49
TECHNIQUE 6 Matching paths to content 49
TECHNIQUE 7 Handling complex paths with wildcards 52
TECHNIQUE 8 URL pattern matching 54
TECHNIQUE 9 Faster routing (without the work) 57
2.4 Summary 58
3 Concurrency in Go 59
3.1 Understanding Go’s concurrency model 59
3.2 Working with goroutines 60
TECHNIQUE 10 Using goroutine closures 61
TECHNIQUE 11 Waiting for goroutines 63
TECHNIQUE 12 Locking with a mutex 67
3.3 Working with channels 72
TECHNIQUE 13 Using multiple channels 73
TECHNIQUE 14 Closing channels 76
TECHNIQUE 15 Locking with buffered channels 80
3.4 Summary 82
PART 2 WELL-ROUNDED APPLICATIONS ........................85
4 Handling errors and panics 87
4.1 Error handling 88
TECHNIQUE 16 Minimize the nils 90
TECHNIQUE 17 Custom error types 92
TECHNIQUE 18 Error variables 93
4.2 The panic system 95
Differentiating panics from errors 96 ■ Working with panics 97
TECHNIQUE 19 Issuing panics 97
Recovering from panics 99
TECHNIQUE 20 Recovering from panics 100
Panics and goroutines 104
TECHNIQUE 21 Trapping panics on goroutines 105
4.3 Summary 111
5 Debugging and testing 113
5.1 Locating bugs 114
Wait, where is my debugger? 114
5.2 Logging 114
Using Go’s logger 115
TECHNIQUE 22 Logging to an arbitrary writer 116
TECHNIQUE 23 Logging to a network resource 118
TECHNIQUE 24 Handling back pressure in network logging 120
Working with system loggers 123
TECHNIQUE 25 Logging to the syslog 123
5.3 Accessing stack traces 126
TECHNIQUE 26 Capturing stack traces 126
5.4 Testing 129
Unit testing 129
TECHNIQUE 27 Using interfaces for mocking or stubbing 130
TECHNIQUE 28 Verifying interfaces with canary tests 132
Generative testing 134
5.5 Using performance tests and benchmarks 136
TECHNIQUE 29 Benchmarking Go code 137
TECHNIQUE 30 Parallel benchmarks 139
TECHNIQUE 31 Detecting race conditions 141
5.6 Summary 142
PART 3 AN INTERFACE FOR YOUR APPLICATIONS.........145
6 HTML and email template patterns 147
6.1 Working with HTML templates 148
Standard library HTML package overview 148 ■ Adding
functionality inside templates 150
TECHNIQUE 32 Extending templates with functions 150
Limiting template parsing 152
TECHNIQUE 33 Caching parsed templates 153
When template execution breaks 154
TECHNIQUE 34 Handling template execution failures 154
Mixing templates 155
TECHNIQUE 35 Nested templates 156
TECHNIQUE 36 Template inheritance 158
TECHNIQUE 37 Mapping data types to templates 161
6.2 Using templates for email 164
TECHNIQUE 38 Generating email from templates 164
6.3 Summary 166
7 Serving and receiving assets and forms 168
7.1 Serving static content 169
TECHNIQUE 39 Serving subdirectories 171
TECHNIQUE 40 File server with custom error pages 172
TECHNIQUE 41 Caching file server 174
TECHNIQUE 42 Embedding files in a binary 176
TECHNIQUE 43 Serving from an alternative location 178
7.2 Handling form posts 180
Introduction to form requests 180
TECHNIQUE 44 Accessing multiple values for a form field 182
Working with files and multipart submissions 183
TECHNIQUE 45 Uploading a single file 183
TECHNIQUE 46 Uploading multiple files 185
TECHNIQUE 47 Verify uploaded file is allowed type 187
Working with raw multipart data 189
TECHNIQUE 48 Incrementally saving a file 189
7.3 Summary 193
8 Working with web services 194
8.1 Using REST APIs 195
Using the HTTP client 195 ■ When faults happen 196
TECHNIQUE 49 Detecting timeouts 197
TECHNIQUE 50 Timing out and resuming with HTTP 198
8.2 Passing and handling errors over HTTP 200
Generating custom errors 201
TECHNIQUE 51 Custom HTTP error passing 201
Reading and using custom errors 203
TECHNIQUE 52 Reading custom errors 204
8.3 Parsing and mapping JSON 206
TECHNIQUE 53 Parsing JSON without knowing the schema 206
8.4 Versioning REST APIs 209
TECHNIQUE 54 API version in the URL 209
TECHNIQUE 55 API version in content type 211
8.5 Summary 213
PART 4 TAKING YOUR APPLICATIONS
TO THE CLOUD..............................................215
9 Using the cloud 217
9.1 What is cloud computing? 218
The types of cloud computing 218 ■ Containers and cloud-native
applications 220
9.2 Managing cloud services 222
Avoiding cloud provider lock-in 222
TECHNIQUE 56 Working with multiple cloud providers 222
Dealing with divergent errors 225
TECHNIQUE 57 Cleanly handling cloud provider errors 225
9.3 Running on cloud servers 227
Performing runtime detection 227
TECHNIQUE 58 Gathering information on the host 227
TECHNIQUE 59 Detecting dependencies 229
Building for the cloud 230
TECHNIQUE 60 Cross-compiling 230
Performing runtime monitoring 232
TECHNIQUE 61 Monitoring the Go runtime 233
9.4 Summary 234
10 Communication between cloud services 235
10.1 Microservices and high availability 236
10.2 Communicating between services 237
Making REST faster 237
TECHNIQUE 62 Reusing connections 238
TECHNIQUE 63 Faster JSON marshal and unmarshal 241
Moving beyond REST 244
TECHNIQUE 64 Using protocol buffers 244
TECHNIQUE 65 Communicating over RPC with protocol
buffers 247
10.3 Summary 252
11 Reflection and code generation 253
11.1 Three features of reflection 254
TECHNIQUE 66 Switching based on type and kind 254
TECHNIQUE 67 Discovering whether a value implements an
interface 258
TECHNIQUE 68 Accessing fields on a struct 262
11.2 Structs, tags, and annotations 266
Annotating structs 266 ■ Using tag annotations 267
TECHNIQUE 69 Processing tags on a struct 268
11.3 Generating Go code with Go code 274
TECHNIQUE 70 Generating code with go generate 275
11.4 Summary 280
index 281
foreword
When I heard that Matt Farina and Matt Butcher were starting a new book on Go, I
was excited. Both have been key contributors in the Go ecosystem for years, and have
extensive work experience and backgrounds that flavor the prose in this book with the
spice of past learnings. The book is intended as a spiritual successor to Go in
Action, taking you beyond the basics that we introduced there and into more practical
learning.
The book is broken into four easily digestible parts, each with a different focus.
Part 1 is a refresher on key Go concepts. If you’re in a hurry and comfortable with
your Go skills, you can safely skip this section, but I discourage that. In reviewing the
final manuscript, I found nuggets of such value that I think everyone would benefit
from these chapters.
Part 2 dives into the mechanics of managing a Go application in the real world.
The chapter on errors is one of the best treatises on Go errors I’ve ever read, and the
chapter on debugging and testing provides useful information on that crucial middle
step of application development that takes your application from proof of concept to
reliable production system.
In part 3, you’ll learn about ways to create user interfaces for your application. The
chapter on templates is an excellent guide to what many find to be a complicated part
of Go’s ecosystem. You’ll see practical ways to reuse your templates and make your
web interfaces more dry. The examples alone are worth the price of the book, as it’s
difficult to find examples of template usage that can be easily mapped to a real-world
application. Later, you’ll see how to create and consume a standards-compliant REST
API and learn the tricks to properly versioning that API.