Book Image

Go: Design Patterns for Real-World Projects

By : Vladimir Vivien, Mario Castro Contreras, Mat Ryer
Book Image

Go: Design Patterns for Real-World Projects

By: Vladimir Vivien, Mario Castro Contreras, Mat Ryer

Overview of this book

The Go programming language has firmly established itself as a favorite for building complex and scalable system applications. Go offers a direct and practical approach to programming that lets programmers write correct and predictable code using concurrency idioms and a full-featured standard library. This practical guide is full of real-world examples to help you get started with Go in no time at all. You’ll start by understanding the fundamentals of Go, then get a detailed description of the Go data types, program structures, and Maps. After that, you’ll learn how to use Go concurrency idioms to avoid pitfalls and create programs that are exact in expected behavior. Next, you will get familiar with the tools and libraries that are available in Go to write and exercise tests, benchmarking, and code coverage. After that, you will be able to utilize some of the most important features of GO such as Network Programming and OS integration to build efficient applications. Then you’ll start applying your skills to build some amazing projects in Go. You will learn to develop high-quality command-line tools that utilize the powerful shell capabilities and perform well using Go’s built-in concurrency mechanisms. Scale, performance, and high availability lie at the heart of our projects, and the lessons learned throughout the sections will arm you with everything you need to build world-class solutions. You will get a feel for app deployment using Docker and Google App Engine. Each project could form the basis of a start-up, which means they are directly applicable to modern software markets. With these skills in hand, you will be able to conquer all your fears of application development and go on to build large, robust and succinct apps in Go. This Learning Path combines some of the best that Packt has to offer in one complete, curated package. It includes content from the following Packt products: 1. Learning Go Programming 2. Go Design Patterns 3. Go Programming Blueprints, Second Edition
Table of Contents (38 chapters)
Go: Design Patterns for Real-World Projects
Credits
Preface
Bibliography

Responding


A big part of any API is responding to requests with a combination of status codes, data, errors, and sometimes headers – the net/http package makes all of this very easy to do. One option we have, which remains the best option for tiny projects or even the early stages of big projects, is to just build the response code directly inside the handler.

As the number of handlers grows, however, we will end up duplicating a lot of code and sprinkling representation decisions all over our project. A more scalable approach is to abstract the response code into helper functions.

For the first version of our API, we are going to speak only JSON, but we want the flexibility to add other representations later if we need to.

Create a new file called respond.go and add the following code:

func decodeBody(r *http.Request, v interface{}) error { 
  defer r.Body.Close() 
  return json.NewDecoder(r.Body).Decode(v) 
} 
func encodeBody(w http.ResponseWriter, r *http.Request, v  interface{}) error {...