Preface

Welcome to the book Go Design Patterns! With this book, you'll learn basic and advanced techniques and patterns with the Go language. Don't worry if you have never written Go code before; this book will gradually introduce you to the various concepts in Go programming. At the same time, experts will find many tips and tricks on the language, so I encourage you to not miss any chapter. If you already know the classic design patterns, you'll find this book very handy, not only as a reference book but also as a way to learn idiomatic Go approaches to solve common problems that you may already know.

The book is divided in three sections:

  • Introduction to the Go language: This is the first part of the book, where you'll learn the basic syntax, the tools that comes with the binary distributions, basic testing, JSON parsing, and more. We leave concurrency for a later chapter to focus on the way that the syntax and the compiler work in a typical Go app.
  • Classic design patterns in idiomatic Go: The second section presents the classic design patterns but as we will see, they are quite different, partly because of the lack of inheritance in Go, but also because we have different and more optimal ways to solve the same problems. A newcomer to the language will find the examples in this section very useful as a way to understand the roots of Go and the idiomatic ways in which you can solve problems using Go in the same manner as you would solve in languages such as Java or C++. Most examples are presented by using TDD and some of them even show examples within Go standard library that uses these patterns.
  • Concurrency patterns: The focus in this section is learning about concurrent structures and parallel execution. You will learn most of the primitives in Go to write concurrent apps, and we will develop some of the classical design patterns with concurrent structures to maximize parallelism. Also, we will learn some of the typical structures to develop concurrent apps in Go. You learn how a classical pattern can become more complex if we need it to work in a concurrent way but the idea is to understand Go concurrent primitives so that the reader finishes the book knowing how to write their own concurrent design patterns by using the knowledge taken from the book.

The book will slowly raise the difficulty of some tasks. We have explained tips and tricks in every chapter.

What this book covers

Chapter 1 , Ready… Steady…Go!, attempts to help newcomers to the Go programming language who have some background in any other programming language. It will begin by showing how to install the Go environment in a Linux machine, moving to syntax, type and flow control.

Chapter 2 , Creational Patterns - Singleton, Builder, Factory, Prototype, and Abstract Factory Design Patterns, introduces the problems that can arise when an object creation or management is particularly complex or expensive using the Singleton, Builder, Factory, and Abstract Factory design patterns.

Chapter 3 , Structural Patterns - Composite, Adapter, and Bridge Design Patterns, deals with the first set of Structural patterns about object composition to get some new functionality. Such as creating an intermediate object and using of various objects as if there is only one.

Chapter 4 , Structural Patterns - Proxy, Facade, Decorator, and Flyweight Design Patterns, is less oriented to multi-object composition but focuses more on obtaining new functionality in existing objects. The Decorator pattern is commonly used to follow the open-closed principle. Facade is extensively used in API’s where you want a single source for many sources of information and actions. Flyweight is not so common but it’s a very useful pattern when the memory is becoming a problem caused by a large collection of similar objects. Finally, the Proxy pattern wraps on an object to provide the same functionality, but at the same time, adding something to the proxy’s functionality.

Chapter 5 ,  Behavioral patterns - Strategy, Chain of Responsibility, Command, and Mediator Design Patterns, deals with the first behavioral pattern to make objects react in an expected or bounded way. We’ll start with the Strategy pattern, perhaps the most important design pattern in object-oriented programming, as many design patterns have something in common with it. Then we’ll move to the Chain of Responsibility to build chains of objects that can decide which between them must deal with a particular case. Finally, Command pattern to encapsulate actions that don’t necessarily need to be executed immediately or must be stored.

Chapter 6 , Behavioral Patterns - Template, Memento, and Interpreter Design Patterns, continues with Behavioral patterns introducing the Interpreter pattern, a quite complex pattern to create small languages and Interpreters for them. It can be very useful when a problem can be solved by inventing a small language for it. The Memento pattern is in front of our eyes every day with the Undo button in apps. The Template pattern helps developers by defining an initial structure of an operation so that the final users of the code can finish it.

Chapter 7 , Behavioral Patterns - Visitor, State, Mediator, and Observer Design Patterns, depicts the Observer pattern, an important pattern that is becoming tremendously popular in distributed systems and reactive programming. The Visitor pattern deals with complex hierarchies of objects where you need to apply a particular action depending on the object. Finally, the State pattern is commonly used in video games and finite state machines and allows an object to change its behavior depending on its own state.

Chapter 8 , Introduction to Go's Concurrency, explains with more detail the CSP concurrency model used in Go by going through some examples using Goroutines and channels, as well as mutexes and syncs.

Chapter 9 , Concurrency Patterns - Barrier, Future, and Pipeline Design Patterns, will introduce some of the CSP concurrency patterns that are idiomatic to the Go language by walking through some examples and explanations. These are small but really powerful patterns so we will provides a few examples of the use of each of them, as well as some schemas (if possible) that will make the understanding of each of them easier.

Chapter 10 ,  Concurrency Patterns -  Workers Pool, and Publish or Subscriber Design Patterns, talks about a couple of patterns with concurrent structures. We will explain every step in detail so you can follow the examples carefully. The idea is to learn patterns to design concurrent applications in idiomatic Go. We are using channels and Goroutines heavily, instead of locks or sharing variables.

..................Content has been hidden....................

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