Curated list of Go design patterns, recipes and idioms
Go Patterns
A curated collection of idiomatic design & application patterns for Go language.
Creational Patterns
| Pattern | Description | Status | |:-------:|:----------- |:------:| | Abstract Factory | Provides an interface for creating families of releated objects | β | | Builder | Builds a complex object using simple objects | β | | Factory Method | Defers instantiation of an object to a specialized function for creating instances | β | | Object Pool | Instantiates and maintains a group of objects instances of the same type | β | | Singleton | Restricts instantiation of a type to one object | β |
Structural Patterns
| Pattern | Description | Status | |:-------:|:----------- |:------:| | Bridge | Decouples an interface from its implementation so that the two can vary independently | β | | Composite | Encapsulates and provides access to a number of different objects | β | | Decorator | Adds behavior to an object, statically or dynamically | β | | Facade | Uses one type as an API to a number of others | β | | Flyweight | Reuses existing instances of objects with similar/identical state to minimize resource usage | β | | Proxy | Provides a surrogate for an object to control it's actions | β |
Behavioral Patterns
| Pattern | Description | Status | |:-------:|:----------- |:------:| | Chain of Responsibility | Avoids coupling a sender to receiver by giving more than object a chance to handle the request | β | | Command | Bundles a command and arguments to call later | β | | Mediator | Connects objects and acts as a proxy | β | | Memento | Generate an opaque token that can be used to go back to a previous state | β | | Observer | Provide a callback for notification of events/changes to data | β | | Registry | Keep track of all subclasses of a given class | β | | State | Encapsulates varying behavior for the same object based on its internal state | β | | Strategy | Enables an algorithm's behavior to be selected at runtime | β | | Template | Defines a skeleton class which defers some methods to subclasses | β | | Visitor | Separates an algorithm from an object on which it operates | β |
Synchronization Patterns
| Pattern | Description | Status | |:-------:|:----------- |:------:| | Condition Variable | Provides a mechanism for threads to temporarily give up access in order to wait for some condition | β | | Lock/Mutex | Enforces mutual exclusion limit on a resource to gain exclusive access | β | | Monitor | Combination of mutex and condition variable patterns | β | | Read-Write Lock | Allows parallel read access, but only exclusive access on write operations to a resource | β | | Semaphore | Allows controlling access to a common resource | β |
Concurrency Patterns
| Pattern | Description | Status | |:-------:|:----------- |:------:| | N-Barrier | Prevents a process from proceeding until all N processes reach to the barrier | β | | Bounded Parallelism | Completes large number of independent tasks with resource limits | β | | Broadcast | Transfers a message to all recipients simultaneously | β | | Coroutines | Subroutines that allow suspending and resuming execution at certain locations | β | | Generators | Yields a sequence of values one at a time | β | | Reactor | Demultiplexes service requests delivered concurrently to a service handler and dispatches them syncronously to the associated request handlers | β | | Parallelism | Completes large number of independent tasks | β | | Producer Consumer | Separates tasks from task executions | β |
Messaging Patterns
| Pattern | Description | Status | |:-------:|:----------- |:------:| | Fan-In | Funnels tasks to a work sink (e.g. server) | β | | Fan-Out | Distributes tasks among workers (e.g. producer) | β | | Futures & Promises | Acts as a place-holder of a result that is initially unknown for synchronization purposes | β | | Publish/Subscribe | Passes information to a collection of recipients who subscribed to a topic | β | | Push & Pull | Distributes messages to multiple workers, arranged in a pipeline | β |
Stability Patterns
| Pattern | Description | Status | |:-------:|:----------- |:------:| | Bulkheads | Enforces a principle of failure containment (i.e. prevents cascading failures) | β | | Circuit-Breaker | Stops the flow of the requests when requests are likely to fail | β | | Deadline | Allows clients to stop waiting for a response once the probability of response becomes low (e.g. after waiting 10 seconds for a page refresh) | β | | Fail-Fast | Checks the availability of required resources at the start of a request and fails if the requirements are not satisfied | β | | Handshaking | Asks a component if it can take any more load, if it can't, the request is declined | β | | Steady-State | For every service that accumulates a resource, some other service must recycle that resource | β |
Profiling Patterns
| Pattern | Description | Status | |:-------:|:----------- |:------:| | Timing Functions | Wraps a function and logs the execution | β |
Idioms
| Pattern | Description | Status | |:-------:|:----------- |:------:| | Functional Options | Allows creating clean APIs with sane defaults and idiomatic overrides | β |
Anti-Patterns
| Pattern | Description | Status | |:-------:|:----------- |:------:| | Cascading Failures | A failure in a system of interconnected parts in which the failure of a part causes a domino effect | β |