Go in Practice: Includes 70 Techniques

Go in Practice: Includes 70 Techniques

by Matt Butcher, Matt Farina

Paperback

$44.99
Choose Expedited Shipping at checkout for guaranteed delivery by Thursday, August 22

Overview

Summary

Go in Practice guides you through 70 real-world techniques in key areas like package management, microservice communication, and more. Following a cookbook-style Problem/Solution/Discussion format, this practical handbook builds on the foundational concepts of the Go language and introduces specific strategies you can use in your day-to-day applications.

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About the Technology

Go may be the perfect systems language. Built with simplicity, concurrency, and modern applications in mind, Go provides the core tool set for rapidly building web, cloud, and systems applications. If you know a language like Java or C#, it's easy to get started with Go; the trick is finding the practical dirt-under-the-fingernails techniques that you need to build production-ready code.

About the Book

Go in Practice guides you through dozens of real-world techniques in key areas. Following a cookbook-style Problem/Solution/Discussion format, this practical handbook builds on the foundational concepts of the Go language and introduces specific strategies you can use in your day-to-day applications. You'll learn techniques for building web services, using Go in the cloud, testing and debugging, routing, network applications, and much more. After finishing this book, you will be ready to build sophisticated cloud-native Go applications.

What's Inside

  • Dozens of specific, practical Golang techniques
  • Using Go for devops and cloudops
  • Writing RESTful web services and microservices
  • Practical web dev techniques

About the Reader

Written for experienced developers who have already started exploring Go and want to use it effectively in a production setting.

About the Authors

Matt Farina is a software architect at Deis. Matt Butcher is a Principal Engineer in the Advanced Technology Group at Hewlett Packard Enterprise. They are both authors, speakers, and regular open source contributors.

Table of Contents

    PART 1 - BACKGROUND AND FUNDAMENTALS
  1. Getting into Go
  2. A solid foundation
  3. Concurrency in Go
  4. PART 2 - WELL-ROUNDED APPLICATIONS
  5. Handling errors and panic
  6. Debugging and testing
  7. PART 3 - AN INTERFACE FOR YOUR APPLICATIONS
  8. HTML and email template patterns
  9. Serving and receiving assets and forms
  10. Working with web services
  11. PART 4 - TAKING YOUR APPLICATIONS TO THE CLOUD
  12. Using the cloud
  13. Communication between cloud services
  14. Reflection and code generation

Product Details

ISBN-13: 9781633430075
Publisher: Manning Publications Company
Publication date: 07/31/2016
Pages: 312
Sales rank: 663,116
Product dimensions: 7.38(w) x 9.25(h) x (d)

About the Author

Matt Butcher is a Master Technologist developing HP Helion, a cloud distribution and public cloud based on OpenStack and Cloud Foundry and he is an author and speaker.

Matt Farina is a Senior Software Engineer at Google's Nest Labs.

Table of 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 modem 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 sewer 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 Accrossing 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 5 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 lag 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

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews