Concurrency in Go
Tools and Techniques for Developers
Katherine Cox-Buday
Concurrency in Go
by Katherine Cox-Buday
Copyright © 2017 Katherine Cox-Buday. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North,
Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional
use. Online editions are also available for most titles (http://oreilly.com/safari).
For more information, contact our corporate/institutional sales department: 800-
998-9938 or corporate@oreilly.com.
Editor: Dawn Schanafelt
Production Editor: Nicholas Adams
Copyeditor: Kim Cofer
Proofreader: Sonia Saruba
Indexer: Judy McConville
Interior Designer: David Futato
Cover Designer: Karen Montgomery
Illustrator: Rebecca Demarest
August 2017: First Edition
Revision History for the First Edition
2017-07-18: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491941195 for release
details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc.
Concurrency in Go, the cover image, and related trade dress are trademarks of
O’Reilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the
information and instructions contained in this work are accurate, the publisher
and the author disclaim all responsibility for errors or omissions, including
without limitation responsibility for damages resulting from the use of or
reliance on this work. Use of the information and instructions contained in this
work is at your own risk. If any code samples or other technology this work
contains or describes is subject to open source licenses or the intellectual
property rights of others, it is your responsibility to ensure that your use thereof
complies with such licenses and/or rights.
978-1-491-94119-5
[LSI]
For L. and N. whose sacrifice made this book possible. Of everything in my life,
you are the best. I love you.
Preface
Hey, welcome to Concurrency in Go! I’m delighted that you’ve picked up this
book and excited to join you in exploring the topic of concurrency in Go over
the next six chapters!
Go is a wonderful language. When it was first announced and birthed into the
world, I remember exploring it with great interest: it was terse, compiled
incredibly fast, performed well, supported duck typing, and — to my delight — I
found working with its concurrency primitives to be intuitive. The first time I
used the go keyword to create a goroutine (something we’ll cover, I promise!) I
got this silly grin on my face. I had worked with concurrency in several
languages, but I had never worked in a language that made concurrency so easy
(which is not to say they don’t exist; I just hadn’t used any). I had found my way
to Go.
Over the years I moved from writing personal scripts in Go, to personal projects,
until I found myself working on a many-hundreds-of-thousands-of-lines project
professionally. Along the way the community was growing with the language,
and we were collectively discovering best practices for working with
concurrency in Go. A few people gave talks on patterns they had discovered. But
there still weren’t many comprehensive guides on how to wield concurrency in
Go in the community.
It was with this in mind that I set out to write this book. I wanted the community
to have access to high-quality and comprehensive information about
concurrency in Go: how to use it, best practices and patterns for incorporating it
into your systems, and how it all works under the covers. I have done my best to
strike a balance between these concerns.
I hope this book proves useful!
Who Should Read This Book
This book is meant for developers who have some experience with Go; I make
no attempt to explain the basic syntax of the language. Knowledge of how
concurrency is presented in other languages is useful, but not necessary.
By the end of this book we will have discussed the entire stack of Go
concurrency concerns: common concurrency pitfalls, motivation behind the
design of Go’s concurrency, the basic syntax of Go’s concurrency primitives,
common concurrency patterns, patterns of patterns, and various tooling that will
help you along the way.
Because of the breadth of topics we’ll cover, this book will be useful to various
cross-sections of people. The next section will help you navigate this book
depending on what needs you have.
Navigating This Book
When I read technical books, I usually hop around to the areas that pique my
interest. Or, if I’m trying to ramp up on a new technology for work, I frantically
skim for the bits that are immediately relevant to my work. Whatever your use
case is, here’s a roadmap for the book with the hopes that it help guide you to
where you need to be!
Chapter 1, An Introduction to Concurrency
This chapter will give you a broad historical perspective on why
concurrency is an important concept, and also discuss some of the
fundamental problems that make concurrency difficult to get correct. It also
briefly touches on how Go helps ease some of this burden.
If you have a working knowledge of concurrency or just want to get to the
technical aspects of how to use Go’s concurrency primitives, it’s safe to
skip this chapter.
Chapter 2, Modeling Your Code: Communicating Sequential Processes
This chapter deals with some of the motivational factors that contributed to
Go’s design. This will help give you some context for conversations with
others in the Go community and help to frame your understanding of why
things work the way they do in the language.
Chapter 3, Go’s Concurrency Building Blocks
Here we’ll start to dig into the syntax of Go’s concurrency primitives. We’ll
also cover the sync package, which is responsible for handling Go’s
memory access synchronization. If you haven’t used concurrency within Go
before and are looking to hop right in, this is the place to start.
Interspersed with the basics of writing concurrent code in Go are
comparisons of concepts to other languages and concurrency models.
Strictly speaking, it’s not necessary to understand these things, but these
concepts help you to achieve a complete understanding on concurrency in
Go.
Chapter 4, Concurrency Patterns in Go
In this chapter, we begin to look at how Go’s concurrency primitives are