Preface

Languages shape the way we think. How we approach problems and formulate solutions for them depends on the concepts we can express using language. This is also true for programming languages. Given a problem, the programs written to solve it may differ from one language to another. This book is about writing programs by expressing concurrent algorithms in the Go language, and about understanding how these programs behave.

Go differs from many popular languages by its emphasis on comprehensibility. This is not the same as readability. Many programs written in easy-to-read languages are not understandable. In the past, I also fell into the trap of writing well-organized programs using frameworks that make programming easy. The problem with that approach is that once writing is over, the program starts a life of its own, and others take over its maintenance. The tribal knowledge that evolved during the development phase is lost, and the team is left with a program that they cannot understand without the help of the last person remaining from the original development team. Developing a program is not that much different from writing a novel. A novel is written so that it can be read by others. So are the programs. If nobody can understand your program, it will not age well.

This book will attempt to explain how to think in the Go language using concurrency constructs so you can understand how the program will behave when you are given a piece of code, and others can understand what you produce. It starts with a high-level overview of concurrency and Go’s treatment of it. It will then work on several data processing problems using concurrent algorithms. After all, programs are written to deal with data. I hope that seeing how concurrency patterns develop organically while solving real-life problems can help you acquire the skills to use the language efficiently and effectively. Later chapters will work on more examples involving timing, periodic tasks, server programming, streaming, and practical uses of atomics. The last chapter will talk about troubleshooting, debugging, and additional instrumentation useful for scalability.

It is impossible to cover all topics related to concurrency in a single book. There are many areas left unexplored. However, I am confident that once you work through the examples, you will have more confidence in solving problems using concurrency. Everybody says concurrency is hard. Using the language correctly makes it easier to produce correct programs. The rule of thumb you should always remember is that correctness comes before performance. So, make it work right first, then you can make it work faster.

Who this book is for

If you are a developer who has basic knowledge of the Go language and are looking to gain expertise in highly concurrent backend application development, this is the book for you. This book would also appeal to Go developers of various experience levels in making their backend systems more robust and scalable.

What this book covers

Chapter 1, Concurrency: A High-Level Overview, talks about what concurrency is and what it isn’t – in particular, how it relates to parallelism. Shared memory and message-passing paradigms, and common concurrency concepts such as race, atomicity, liveness, and deadlock are also introduced in this chapter.

Chapter 2, Go Concurrency Primitives, introduces Go language primitives for concurrent programming – namely, goroutines, channels, mutexes, wait groups, and condition variables.

Chapter 3, The Go Memory Model, talks about the visibility guarantees of memory operations. It introduces the happened-before relationship that allows you to reason about concurrent behavior, then gives the memory visibility guarantees of concurrency primitives and some of the standard library facilities.

Chapter 4, Some Well-Known Concurrency Problems, studies the well-known producer/consumer problem, the dining philosophers problem, and rate-limiting.

Chapter 5, Worker Pools and Pipelines, first studies worker pools, which is a common way to process large amounts of data with limited concurrency. Then, it develops several concurrent data pipeline implementations for efficient data processing applications.

Chapter 6, Error Handling, explores how to deal with errors and panics in a concurrent program, and how to pass errors around.

Chapter 7, Timers and Tickers, shows how to do things periodically and how to do things some time later.

Chapter 8, Handling Requests Concurrently, mostly talks about server programming, but many of the concepts discussed in this chapter are broadly about handling requests, so they can be applied in a wide range of scenarios. It describes how to use context effectively, how to distribute work and collect results, how to limit concurrency, and how to stream data.

Chapter 9, Atomic Memory Operations, covers atomic memory operations, their memory guarantees, and their practical uses.

Chapter 10, Troubleshooting Concurrency Issues, talks about the underrated but essential skill of reading stack traces, and how to detect failures and heal them at runtime.

To get the most out of this book

You need to have a basic understanding of the Go language and a running Go development environment for your operating system. This book does not rely on any other third-party tools or libraries. Use the code editor you are most comfortable with. All examples and code samples can be built and run using the Go build system.

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Effective-Concurrency-in-Go. If there’s an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: https://packt.link/3rxJ9.

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “The net/http package implements a Server type that handles each request in a separate goroutine.”

A block of code is set as follows:

1: chn := make(chan bool) // Create an unbuffered channel
2: go func() {
3:     chn <- true  // Send to channel
4: }()
5: go func() {
6:     var y bool
7:     y <-chn      // Receive from channel
8:     fmt.Println(y)
9: }()

Any command-line input or output is written as follows:

{"row":65,"height":172.72,"weight":97.61} {"row":64,"height":195.58,"weight":81.266} {"row":66,"height":142.24,"weight":101.242} {"row":68,"height":152.4,"weight":80.358} {"row":67,"height":162.56,"weight":104.87400000000001}

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at [email protected] and mention the book title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share Your Thoughts

Once you’ve read Concurrency in Golang, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere? Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below
Free eBook PDF QR Code

https://packt.link/free-ebook/9781804619070

  1. Submit your proof of purchase
  2. That’s it! We’ll send your free PDF and other benefits to your email directly
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.121.160