Go in Practice
Matt Butcher Matt Farina
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email:
orders@manning.com2016 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
| Manning Publications Co.20 Baldwin RoadPO Box 761Shelter Island, NY 11964 |
Development editor: Susanna KlineTechnical development editors: Ivan Kirkpatrick, Kim Shrier,Glenn Burnside, Alain CouniotReview editor: Aleksandar DragosavljevicProject editor: Karen GulliverCopy editor: Sharon WilkeyProofreader: Melody DolabTechnical Proofreader: James FraschTypesetter: Dottie MarsicoCover designer: Marija Tudor
ISBN 9781633430075
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 EBM 21 20 19 18 17 16
Brief Table of Contents
Table of Contents
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. is a refresher on key Go concepts. If youre 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.
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 Ive 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 , youll 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 Gos ecosystem. Youll 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 its difficult to find examples of template usage that can be easily mapped to a real-world application. Later, youll see how to create and consume a standards-compliant REST API and learn the tricks to properly versioning that API.
The final section of the book moves into the interoperability layer thats required in nearly every application today. Youll dive deep into cloud infrastructure and see where Go fits in the cloud-computing model. Youll finish with great coverage of microservices and service-to-service communication patterns.
Whether youre just coming to Go or youve been writing Go applications for years, this book has vital knowledge that will help you take your application development to the next level. The authors do a great job of presenting complex information with a unified voice and in a manner thats easy to digest. Im excited for the publication of this book and the value that it brings to the Go community. I hope that youll enjoy reading it as much as I have.
B RIAN K ETELSEN
C O-AUTHOR OF G O IN A CTION
C O-FOUNDER OF G OPHER A CADEMY
Preface
When we first started using Go, we saw a language with a lot of potential. We wanted to build applications with it. But it was a new language, and many companies are wary of introducing a new programming language.
This is especially true in the enterprise, where Go has the potential to have a huge impact. New languages are challenged to be trusted, accepted, and adopted. There are hundreds of thousands of developers in businesses where leaders need to be swayed to try a new language and developers need to learn it well enough to build applications and see a benefit.
Open source projects, conferences, training, and books all help to make a programming language more palatable. We wanted to write a book that teaches Go in order to help the Go community, help those trying to learn Go or to convince their organizations leadership, and help us in the companies that we work for and with.
When we first started the book, it was targeted squarely at cloud development with Go. Go is a language built for the cloud, and weve spent years working in cloud computing. Once we started working with Manning Publications, we saw an opportunity to expand beyond the cloud, into more useful and helpful patterns. And so the book shifted from being cloud-focused to pattern-focused. Yet it still retains its cloud roots.
Go in Practice is our attempt to help developers move from being familiar with the language to being productive with it. To help the community of developers grow, while helping organizations write better software.
Acknowledgments
Weve spent about two years writing this book, but none of the effort would have been possible without the commitment of our families. Theyve supported us through the early mornings, late nights, and weekends when we were focused on writing. They were there as we were fixated on solving problems, even when we werent sitting down to write.
Good code is never created in a vacuum. Were also grateful to the women and men of the Go community who have so generously given their time to create a great language, great libraries, and a thriving ecosystem. It has been exciting to be a part of such a diverse, burgeoning community of developers. In particular, Rob Pike, Brian Ketelsen, and Dave Cheney all reached out to us early in our Go learning process. Theyre admirable ambassadors of the language. Special thanks to Brian for contributing the foreword to the book and for endorsing our work.
We appreciate the many individuals who gave time and effort to the creation of this book. It has been an arduous process, and thanks to many careful readers, including our MEAP readers, we found and corrected numerous mistakes.
Wed like to thank everyone at Manning, especially our development editor, Susanna Kline; our technical development editors, Ivan Kirkpatrick, Kim Shrier, Glenn Burnside, and Alain Couniot; and our technical proofreader, James Frasch; as well as everyone who worked on our book behind the scenes. Thanks also to the many reviewers who took the time to read our manuscript at various stages of its development and who provided invaluable feedback: Anthony Cramp, Austin Riendeau, Brandon Titus, Doug Sparling, Ferdinando Santacroce, Gary A. Stafford, Jim Amrhein, Kevin Martin, Nathan Davies, Quintin Smith, Sam Zaydel, and Wes Shaddix.