Mastering Concurrency in Go

Mastering Concurrency in Go
eBook: $30.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 15%!
Print + free eBook + free PacktLib access to the book: $81.98    Print cover: $50.99
save 38%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Table of Contents
Sample Chapters
  • Explore the core syntaxes and language features that enable concurrency in Go
  • Understand when and where to use concurrency to keep data consistent and applications non-blocking, responsive, and reliable
  • A practical approach to utilize application scaffolding to design highly-scalable programs that are deeply rooted in go routines and channels

Book Details

Language : English
Paperback : 328 pages [ 235mm x 191mm ]
Release Date : July 2014
ISBN : 1783983485
ISBN 13 : 9781783983483
Author(s) : Nathan Kozyra
Topics and Technologies : All Books, Application Development, Open Source

Table of Contents

Chapter 1: An Introduction to Concurrency in Go
Chapter 2: Understanding the Concurrency Model
Chapter 3: Developing a Concurrent Strategy
Chapter 4: Data Integrity in an Application
Chapter 5: Locks, Blocks, and Better Channels
Chapter 6: C10K – A Non-blocking Web Server in Go
Chapter 7: Performance and Scalability
Chapter 8: Concurrent Application Architecture
Chapter 9: Logging and Testing Concurrency in Go
Chapter 10: Advanced Concurrency and Best Practices
  • Understanding goroutines versus coroutines
  • Implementing channels
    • Channel-based sorting at the letter capitalization factory
    • Cleaning up our goroutines
      • Buffered or unbuffered channels
    • Using the select statement
  • Closures and goroutines
  • Building a web spider using goroutines and channels
  • Summary
  • Chapter 2: Understanding the Concurrency Model
    • Understanding the working of goroutines
    • Synchronous versus asynchronous goroutines
      • Designing the web server plan
    • Visualizing concurrency
    • RSS in action
      • An RSS reader with self diagnostics
      • Imposing a timeout
    • A little bit about CSP
      • The dining philosophers problem
    • Go and the actor model
    • Object orientation
      • Demonstrating simple polymorphism in Go
    • Using concurrency
    • Managing threads
    • Using sync and mutexes to lock data
    • Summary
  • Chapter 3: Developing a Concurrent Strategy
    • Applying efficiency in complex concurrency
    • Identifying race conditions with race detection
      • Using mutual exclusions
      • Exploring timeouts
        • Importance of consistency
    • Synchronizing our concurrent operations
    • The project – multiuser appointment calendar
      • Visualizing a concurrent pattern
      • Developing our server requirements
        • Web server
        • Using templates
        • Time
      • Endpoints
      • Custom structs
    • A multiuser Appointments Calendar
    • A note on style
    • A note on immutability
    • Summary
  • Chapter 4: Data Integrity in an Application
    • Getting deeper with mutexes and sync
    • The cost of goroutines
    • Working with files
    • Getting low – implementing C
      • Touching memory in cgo
      • The structure of cgo
      • The other way around
        • Getting even lower – assembly in Go
    • Distributed Go
    • Some common consistency models
      • Distributed shared memory
      • First-in-first-out – PRAM
      • Looking at the master-slave model
      • The producer-consumer problem
      • Looking at the leader-follower model
      • Atomic consistency / mutual exclusion
      • Release consistency
    • Using memcached
      • Circuit
    • Summary
  • Chapter 5: Locks, Blocks, and Better Channels
    • Understanding blocking methods in Go
      • Blocking method 1 – a listening, waiting channel
        • Sending more data types via channels
        • The net package – a chat server with interfaced channels
      • Examining our client
      • Blocking method 2 – the select statement in a loop
    • Cleaning up goroutines
      • Blocking method 3 – network connections and reads
    • Creating channels of channels
    • Pprof – yet another awesome tool
    • Handling deadlocks and errors
    • Summary
  • Chapter 6: C10K – A Non-blocking Web Server in Go
    • Attacking the C10K problem
      • Failing of servers at 10,000 concurrent connections
      • Using concurrency to attack C10K
      • Taking another approach
    • Building our C10K web server
      • Benchmarking against a blocking web server
      • Handling requests
      • Routing requests
    • Serving pages
      • Parsing our template
      • External dependencies
        • Connecting to MySQL
    • Multithreading and leveraging multiple cores
    • Exploring our web server
      • Timing out and moving on
    • Summary
  • Chapter 7: Performance and Scalability
    • High performance in Go
      • Getting deeper into pprof
      • Parallelism's and concurrency's impact on I/O pprof
    • Using the App Engine
    • Distributed Go
      • Types of topologies
        • Type 1 – star
        • Type 2 – mesh
        • The Publish and Subscribe model
        • Serialized data
        • Remote code execution
        • Other topologies
        • Message Passing Interface
    • Some helpful libraries
      • Nitro profiler
      • Heka
      • GoFlow
    • Memory preservation
      • Garbage collection in Go
    • Summary
  • Chapter 8: Concurrent Application Architecture
    • Designing our concurrent application
    • Identifying our requirements
    • Using NoSQL as a data store in Go
      • MongoDB
      • Redis
      • Tiedot
      • CouchDB
      • Cassandra
      • Couchbase
      • Setting up our data store
    • Monitoring filesystem changes
    • Managing logfiles
    • Handling configuration files
    • Detecting file changes
      • Sending changes to clients
      • Checking records against Couchbase
    • Backing up our files
    • Designing our web interface
    • Reverting a file's history – command line
      • Using Go in daemons and as a service
    • Checking the health of our server
    • Summary
  • Chapter 9: Logging and Testing Concurrency in Go
    • Handling errors and logging
      • Breaking out goroutine logs
      • Using the LiteIDE for richer and easier debugging
      • Sending errors to screen
      • Logging errors to file
      • Logging errors to memory
    • Using the log4go package for robust logging
      • Panicking
      • Recovering
        • Logging our panics
      • Catching stack traces with concurrent code
    • Using the runtime package for granular stack traces
    • Summary
  • Chapter 10: Advanced Concurrency and Best Practices
    • Going beyond the basics with channels
    • Building workers
    • Implementing nil channel blocks
      • Using nil channels
    • Implementing more granular control over goroutines with tomb
    • Timing out with channels
    • Building a load balancer with concurrent patterns
    • Choosing unidirectional and bidirectional channels
      • Using receive-only or send-only channels
    • Using an indeterminate channel type
    • Using Go with unit testing
      • GoCheck
      • Ginkgo and Gomega
    • Using Google App Engine
    • Utilizing best practices
      • Structuring your code
      • Documenting your code
      • Making your code available via go get
      • Keeping concurrency out of your packages
    • Summary

Nathan Kozyra

Nathan Kozyra has been programming both recreationally and professionally for more than a decade now. His first experience came while writing games on his grandfather's Commodore 64, and in the ensuing decades, he's crafted technological solutions and applications in nearly every major language for a host of software and media companies as a developer, advisor, creative technologist, and CTO. He is currently the CTO of Pointslocal. A new-language enthusiast and C++ stalwart, his attention was quickly captured by Google's Go language, both for the language's creators and ethos as well as its apparent post-C approach to systems languages. Having dived in quickly, Go is now his go-to language for fast, concurrent applications.

Sorry, we don't have any reviews for this title yet.

Code Downloads

Download the code and support files for this book.

Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.

Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

Mastering Concurrency in Go +    IBM Cognos 8 Report Studio Cookbook =
50% Off
the second eBook
Price for both: $46.10

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • Create goroutines as the building blocks of concurrency in Go
  • Extend the goroutines to include channels to open up communication
  • Design concurrent patterns to apply toward our examples and future applications
  • Explore methods to ensure data consistency remains paramount in concurrent and multi-core Go applications
  • Tune performance to squeeze the most out of existing hardware to keep your applications below resource thresholds
  • Utilize advanced concurrency patterns and best practices to stay low-level without compromising the simplicity of Go itself
  • Build several servers in Go that strongly utilize concurrency features

In Detail

This book will take you through the history of concurrency, how Go utilizes it, how Go differs from other languages, and the features and structures of Go's concurrency core. Each step of the way, the book will present real, usable examples with detailed descriptions of the methodologies used. By the end, you will feel comfortable designing a safe, data-consistent, high-performance concurrent application in Go.

The focus of this book is on showing you how Go can be used to program high-performance, robust concurrent programs with Go's unique form of multithreading, which employs goroutines that communicate and synchronize across channels. Designed for any curious developer or systems administrator with an interest in fast, non-blocking, and resource-thrifty systems applications, this book is an invaluable resource to help you understand Go's powerful concurrency focus.


A practical approach covering everything you need to know to get up and running with Go, starting with the basics and imparting increasingly more detail as the examples and topics become more complicated. The book utilizes a casual, conversational style, rife with actual code and historical anecdotes for perspective, as well as usable and extensible example applications.

Who this book is for

This book is intended for systems developers and programmers with some experience in either Go and/or concurrent programming who wish to become fluent in building high-performance applications that scale by leveraging single-core, multicore, or distributed concurrency.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software