Network Programming with Go. 2021 by Adam Woodbeck
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
ISBN-13: 978-1-7185-0088-4 (print)
ISBN-13: 978-1-7185-0089-1 (ebook)
Publisher: William Pollock
Executive Editor: Barbara Yien
Production Editor: Kate Kaminski
Developmental Editor: Frances Saux
Cover Illustration: Gina Redman
Interior Design: Octopod Studios
Technical Reviewer: Jeremy Bowers
Copyeditor: Sharon Wilkey
Compositor: Jeff Lytle, Happenstance Type-O-Rama
Proofreader: Paula L. Fleming
For information on book distributors or translations, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1-415-863-9900;
www.nostarch.com
Library of Congress Control Number: 2020943331
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
For my wife, Mandy, and my children, Benjamin and Lilyanna
About the Author
Adam Woodbeck is a senior software engineer at Barracuda Networks, where he implemented a distributed cloud environment in Go to supplant the previous cloud infrastructure, profoundly increasing its scalability and performance. Hes since served as the architect for many network-based services in Go.
About the Technical Reviewer
Jeremy Bowers is a distinguished software architect in the Office of CTO at Barracuda Networks. Equipped with many years of lead developer experience at Barracuda and security startups, especially in network engineering, Jeremy has successfully designed and implemented services that efficiently serve hundreds of thousands of customers worldwide. He holds a bachelors and a masters degree in computer science from Michigan State University.
Acknowledgments
Ive never played in a rock band, but I imagine writing this book is a bit like that. I may have been the singer-songwriter, but this book would have been noticeably inferior had it not been for the exceptional talents and support of the following people.
Jeremy Bowers is one of the most talented engineers and enjoyable human beings Ive had the pleasure of knowing. The depth and breadth of his knowledge considerably eased my impostor syndrome, knowing that he staked his reputation and likely his career on the success of this book. He reviewed every paragraph and line of code to ensure their coherence and accuracy. But as with any large pull request, the responsibility for any bugs lies with me, and despite Jeremys best efforts, Ive been known to write some profoundly clever bugs. Thank you, Jeremy, for contributing your technical expertise to this book.
I dont know how much editing my writing required compared to the average author, but judging by the red markup on my drafts, Frances Saux is a saint. She shepherded me through this process and was an outstanding advocate for the reader. I could rely on her to keep my writing conversational and ask pointed questions that prompted me to elaborate on topics I take for granted. Thank you, Frances, for your patience and consistency throughout the writing process. This book certainly wouldnt be the same without your extensive efforts.
I would also like to thank Bill Pollock for giving this book his blessing; Barbara Yien for supervising it; Sharon Wilkey and Kate Kaminski for their copyediting and production expertise, respectively; Paula Fleming for proofreading; Derek Yee for the books cover and interior design; Gina Redman for the cover illustration; and Matt Holt for reviewing Chapter 10 for technical accuracy.
Most of all, Id like to thank my wife, Amandalyn; my son, Benjamin; and my daughter, Lilyanna. As with any extracurricular endeavor, the research and writing process consumed a lot of our family time. But Im thankful for your patience while I strived to find a balance that worked for our family during this undertaking. Your love and support allowed me to step outside my comfort zone. Hopefully, my example will encourage you to do the same.
Introduction
With the advent of the internet came an ever-increasing demand for network engineers and developers. Today, personal computers, tablets, phones, televisions, watches, gaming systems, vehicles, common household items, and even doorbells communicate over the internet. Network programming makes all this possible. And secure network programming makes it trustworthy, driving increasing numbers of people to adopt these services. This book will teach you how to write contemporary network software using Gos asynchronous features.
Google created the Go programming language in 2007 to increase the productivity of developers working with large code bases. Since then, Go has earned a reputation as a fast, efficient, and safe language for the development and deployment of software at some of the largest companies in the world. Go is easy to learn and has a rich standard library, well suited for taking advantage of multicore, networked systems.
This book details the basics of network programming with an emphasis on security. You will learn socket-level programming including TCP, UDP, and Unix sockets, interact with application-level protocols like HTTPS and HTTP/2, serialize data with formats like Gob, JSON, XML, and protocol buffers, perform authentication and authorization for your network services, create streams and asynchronous data transfers, write gRPC microservices, perform structured logging and instrumentation, and deploy your applications to the cloud.
At the end of our journey, you should feel comfortable using Go, its standard library, and popular third-party packages to design and implement secure network applications and microservices. Every chapter uses best practices and includes nuggets of wisdom that will help you avoid potential pitfalls.
Who This Book Is For
If youd like to learn how to securely share data over a network using standard protocols, all the while writing Go code that is stable, secure, and effective, this book is for you.
The target reader is a security-conscious developer or system administrator who wishes to take a deep dive into network programming and has a working knowledge of Go and Gos module support. That said, the first few chapters introduce basic networking concepts, so networking newcomers are welcome.
Staying abreast of contemporary protocols, standards, and best practices when designing and developing network applications can be difficult. Thats why, as you work through this book, youll be given increased responsibility. Youll also be introduced to tools and tech that will make your workload manageable.
Next page