O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Go Design Patterns

Book Description

Learn idiomatic, efficient, clean, and extensible Go design and concurrency patterns by using TDD

About This Book

  • A highly practical guide filled with numerous examples unleashing the power of design patterns with Go.
  • Discover an introduction of the CSP concurrency model by explaining GoRoutines and channels.
  • Get a full explanation, including comprehensive text and examples, of all known GoF design patterns in Go.

Who This Book Is For

The target audience is both beginner- and advanced-level developers in the Go programming language. No knowledge of design patterns is expected.

What You Will Learn

  • All basic syntax and tools needed to start coding in Go
  • Encapsulate the creation of complex objects in an idiomatic way in Go
  • Create unique instances that cannot be duplicated within a program
  • Understand the importance of object encapsulation to provide clarity and maintainability
  • Prepare cost-effective actions so that different parts of the program aren’t affected by expensive tasks
  • Deal with channels and GoRoutines within the Go context to build concurrent application in Go in an idiomatic way

In Detail

Go is a multi-paradigm programming language that has built-in facilities to create concurrent applications. Design patterns allow developers to efficiently address common problems faced during developing applications.

Go Design Patterns will provide readers with a reference point to software design patterns and CSP concurrency design patterns to help them build applications in a more idiomatic, robust, and convenient way in Go.

The book starts with a brief introduction to Go programming essentials and quickly moves on to explain the idea behind the creation of design patterns and how they appeared in the 90’s as a common "language" between developers to solve common tasks in object-oriented programming languages. You will then learn how to apply the 23 Gang of Four (GoF) design patterns in Go and also learn about CSP concurrency patterns, the "killer feature" in Go that has helped Google develop software to maintain thousands of servers.

With all of this the book will enable you to understand and apply design patterns in an idiomatic way that will produce concise, readable, and maintainable software.

Style and approach

This book will teach widely used design patterns and best practices with Go in a step-by-step manner. The code will have detailed examples, to allow programmers to apply design patterns in their day-to-day coding.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Go Design Patterns
    1. Go Design Patterns
    2. Credits
    3. About the Author
    4. About the Reviewer
    5. www.PacktPub.com
      1. Why subscribe?
    6. Customer Feedback
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Ready... Steady... Go!
      1. A little bit of history
      2. Installing Go
        1. Linux
          1. Go Linux advanced installation
        2. Windows
        3. Mac OS X
        4. Setting the workspace - Linux and Apple OS X
      3. Starting with Hello World
      4. Integrated Development Environment - IDE
      5. Types
      6. Variables and constants
      7. Operators
      8. Flow control
        1. The if... else statement
        2. The switch statement
        3. The for…range statement
      9. Functions
        1. What does a function look like?
        2. What is an anonymous function?
        3. Closures
        4. Creating errors, handling errors and returning errors.
        5. Function with undetermined number of parameters
          1. Naming returned types
      10. Arrays, slices, and maps
        1. Arrays
          1. Zero-initialization
        2. Slices
        3. Maps
      11. Visibility
      12. Zero-initialization
      13. Pointers and structures
        1. What is a pointer? Why are they good?
        2. Structs
      14. Interfaces
        1. Interfaces - signing a contract
      15. Testing and TDD
        1. The testing package
        2. What is TDD?
      16. Libraries
      17. The Go get tool
      18. Managing JSON data
        1. The encoding package
      19. Go tools
        1. The golint tool
        2. The gofmt tool
        3. The godoc tool
        4. The goimport tool
      20. Contributing to Go open source projects in GitHub
      21. Summary
    9. 2. Creational Patterns - Singleton, Builder, Factory, Prototype, and Abstract Factory Design Patterns
      1. Singleton design pattern - having a unique instance of a type in the entire program
        1. Description
        2. Objectives
        3. Example - a unique counter
        4. Requirements and acceptance criteria
        5. Writing unit tests first
        6. Implementation
        7. A few words about the Singleton design pattern
      2. Builder design pattern - reusing an algorithm to create many implementations of an interface
        1. Description
        2. Objectives
        3. Example - vehicle manufacturing
        4. Requirements and acceptance criteria
        5. Unit test for the vehicle builder
        6. Implementation
        7. Wrapping up the Builder design pattern
      3. Factory method - delegating the creation of different types of payments
        1. Description
        2. Objectives
        3. The example - a factory of payment methods for a shop
        4. Acceptance criteria
        5. First unit test
        6. Implementation
        7. Upgrading the Debitcard method to a new platform
        8. What we learned about the Factory method
      4. Abstract Factory - a factory of factories
        1. Description
        2. The objectives
        3. The vehicle factory example, again?
        4. Acceptance criteria
        5. Unit test
        6. Implementation
        7. A few lines about the Abstract Factory method
      5. Prototype design pattern
        1. Description
        2. Objective
        3. Example
        4. Acceptance criteria
        5. Unit test
        6. Implementation
        7. What we learned about the Prototype design pattern
      6. Summary
    10. 3. Structural Patterns - Composite, Adapter, and Bridge Design Patterns
      1. Composite design pattern
        1. Description
        2. Objectives
        3. The swimmer and the fish
        4. Requirements and acceptance criteria
        5. Creating compositions
        6. Binary Tree compositions
        7. Composite pattern versus inheritance
        8. Final words on the Composite pattern
      2. Adapter design pattern
        1. Description
        2. Objectives
        3. Using an incompatible interface with an Adapter object
        4. Requirements and acceptance criteria
        5. Unit testing our Printer adapter
        6. Implementation
        7. Examples of the Adapter pattern in Go's source code
        8. What the Go source code tells us about the Adapter pattern
      3. Bridge design pattern
        1. Description
        2. Objectives
        3. Two printers and two ways of printing for each
        4. Requirements and acceptance criteria
        5. Unit testing the Bridge pattern
        6. Implementation
        7. Reuse everything with the Bridge pattern
      4. Summary
    11. 4. Structural Patterns - Proxy, Facade, Decorator, and Flyweight Design Patterns
      1. Proxy design pattern
        1. Description
        2. Objectives
        3. Example
        4. Acceptance criteria
        5. Unit test
        6. Implementation
        7. Proxying around actions
      2. Decorator design pattern
        1. Description
        2. Objectives
        3. Example
        4. Acceptance criteria
        5. Unit test
        6. Implementation
        7. A real-life example - server middleware
          1. Starting with the common interface, http.Handler
        8. A few words about Go's structural typing
        9. Summarizing the Decorator design pattern - Proxy versus Decorator
      3. Facade design pattern
        1. Description
        2. Objectives
        3. Example
        4. Acceptance criteria
        5. Unit test
        6. Implementation
        7. Library created with the Facade pattern
      4. Flyweight design pattern
        1. Description
        2. Objectives
        3. Example
        4. Acceptance criteria
        5. Basic structs and tests
        6. Implementation
        7. What's the difference between Singleton and Flyweight then?
      5. Summary
    12. 5. Behavioral Patterns - Strategy, Chain of Responsibility, and Command Design Patterns
      1. Strategy design pattern
        1. Description
        2. Objectives
        3. Rendering images or text
        4. Acceptance criteria
        5. Implementation
        6. Solving small issues in our library
        7. Final words on the Strategy pattern
      2. Chain of responsibility design pattern
        1. Description
        2. Objectives
        3. A multi-logger chain
        4. Unit test
        5. Implementation
        6. What about a closure?
        7. Putting it together
      3. Command design pattern
        1. Description
        2. Objectives
        3. A simple queue
        4. Acceptance criteria
        5. Implementation
        6. More examples
        7. Chain of responsibility of commands
        8. Rounding-up the Command pattern up
      4. Summary
    13. 6. Behavioral Patterns - Template, Memento, and Interpreter Design Patterns
      1. Template design pattern
        1. Description
        2. Objectives
        3. Example - a simple algorithm with a deferred step
        4. Requirements and acceptance criteria
        5. Unit tests for the simple algorithm
        6. Implementing the Template pattern
        7. Anonymous functions
        8. How to avoid modifications on the interface
        9. Looking for the Template pattern in Go's source code
        10. Summarizing the Template design pattern
      2. Memento design pattern
        1. Description
        2. Objectives
        3. A simple example with strings
        4. Requirements and acceptance criteria
        5. Unit test
        6. Implementing the Memento pattern
        7. Another example using the Command and Facade patterns
        8. Last words on the Memento pattern
      3. Interpreter design pattern
        1. Description
        2. Objectives
        3. Example - a polish notation calculator
        4. Acceptance criteria for the calculator
        5. Unit test of some operations
        6. Implementation
        7. Complexity with the Interpreter design pattern
        8. Interpreter pattern again - now using interfaces
        9. The power of the Interpreter pattern
      4. Summary
    14. 7. Behavioral Patterns - Visitor, State, Mediator, and Observer Design Patterns
      1. Visitor design pattern
        1. Description
        2. Objectives
        3. A log appender
        4. Acceptance criteria
        5. Unit tests
        6. Implementation of Visitor pattern
        7. Another example
        8. Visitors to the rescue!
      2. State design pattern
        1. Description
        2. Objectives
        3. A small guess the number game
        4. Acceptance criteria
        5. Implementation of State pattern
        6. A state to win and a state to lose
        7. The game built using the State pattern
      3. Mediator design pattern
        1. Description
        2. Objectives
        3. A calculator
        4. Acceptance criteria
        5. Implementation
        6. Uncoupling two types with the Mediator
      4. Observer design pattern
        1. Description
        2. Objectives
        3. The notifier
        4. Acceptance criteria
        5. Unit tests
        6. Implementation
        7. Summary
    15. 8. Introduction to Gos Concurrency
      1. A little bit of history and theory
        1. Concurrency versus parallelism
        2. CSP versus actor-based concurrency
      2. Goroutines
        1. Our first Goroutine
        2. Anonymous functions launched as new Goroutines
        3. WaitGroups
      3. Callbacks
        1. Callback hell
      4. Mutexes
        1. An example with mutexes - concurrent counter
        2. Presenting the race detector
      5. Channels
        1. Our first channel
        2. Buffered channels
        3. Directional channels
        4. The select statement
        5. Ranging over channels too!
      6. Using it all - concurrent singleton
        1. Unit test
        2. Implementation
      7. Summary
    16. 9. Concurrency Patterns - Barrier, Future, and Pipeline Design Patterns
      1. Barrier concurrency pattern
        1. Description
        2. Objectives
        3. An HTTP GET aggregator
        4. Acceptance criteria
        5. Unit test - integration
        6. Implementation
        7. Waiting for responses with the Barrier design pattern
      2. Future design pattern
        1. Description
        2. Objectives
        3. A simple asynchronous requester
        4. Acceptance criteria
        5. Unit tests
        6. Implementation
        7. Putting the Future together
      3. Pipeline design pattern
        1. Description
        2. Objectives
        3. A concurrent multi-operation
        4. Acceptance criteria
        5. Beginning with tests
        6. Implementation
          1. The list generator
          2. Raising numbers to the power of 2
          3. Final reduce operation
          4. Launching the Pipeline pattern
        7. Final words on the Pipeline pattern
      4. Summary
    17. 10. Concurrency Patterns - Workers Pool and Publish/Subscriber Design Patterns
      1. Workers pool
        1. Description
        2. Objectives
        3. A pool of pipelines
        4. Acceptance criteria
        5. Implementation
          1. The dispatcher
          2. The pipeline
        6. An app using the workers pool
        7. No tests?
        8. Wrapping up the Worker pool
      2. Concurrent Publish/Subscriber design pattern
        1. Description
        2. Objectives
        3. Example - a concurrent notifier
        4. Acceptance criteria
        5. Unit test
          1. Testing subscriber
          2. Testing publisher
        6. Implementation
          1. Implementing the publisher
          2. Handling channels without race conditions
        7. A few words on the concurrent Observer pattern
      3. Summary