2020-06-19 11:34:51 +03:00
![gopher ](gopher.png )
2016-01-01 22:06:10 +03:00
< p align = "center" >
2016-09-13 14:42:18 +03:00
< h1 align = "center" >
Go Patterns
< br >
2017-01-31 17:42:07 +03:00
< a href = "http://travis-ci.org/tmrts/go-patterns" > < img alt = "build-status" src = "https://img.shields.io/badge/build-passing-brightgreen.svg?style=flat-square" / > < / a >
< a href = "https://github.com/sindresorhus/awesome" > < img alt = "awesome" src = "https://img.shields.io/badge/awesome-%E2%9C%93-ff69b4.svg?style=flat-square" / > < / a >
2020-06-19 11:34:51 +03:00
< a href = "https://github.com/crazybber/awesome-patterns/blob/master/LICENSE" > < img alt = "license" src = "https://img.shields.io/badge/license-Apache%20License%202.0-E91E63.svg?style=flat-square" / > < / a >
2016-09-13 14:42:18 +03:00
< / h1 >
2016-01-01 22:06:10 +03:00
< / p >
A curated collection of idiomatic design & application patterns for Go language.
2015-12-15 01:31:23 +03:00
2020-05-27 16:53:18 +03:00
#### a fork of [tmrts/go-patterns](https://github.com/tmrts/go-patterns) but merged in with other awesome forks. can go with [patterns-in-examples](https://github.com/crazybber/go-pattern-examples)
2020-05-06 12:14:54 +03:00
2020-05-03 18:42:10 +03:00
2020-05-08 14:46:54 +03:00
## Merged Repos
2020-05-02 20:40:25 +03:00
2020-05-02 20:49:39 +03:00
+ *Merged : https://github.com/nynicg/go-patterns*
2020-05-06 12:24:11 +03:00
+ *Merged : https://github.com/weichou1229/go-patterns*
2020-05-02 20:49:39 +03:00
+ *Merged : https://github.com/jianhan/go-patterns*
2020-05-02 20:56:19 +03:00
+ *Merged : https://github.com/sakari-ai/go-patterns*
+ *Merged : https://github.com/restudy/go-patterns*
2020-05-02 20:40:25 +03:00
2016-09-13 14:42:18 +03:00
## Creational Patterns
2015-12-15 01:31:23 +03:00
2016-09-13 15:41:40 +03:00
| Pattern | Description | Status |
|:-------:|:----------- |:------:|
2020-05-06 12:24:11 +03:00
| [Abstract Factory ](/creational/abstract_factory.md ) | Provides an interface for creating families of releated objects | ✔ |
2020-05-02 20:40:25 +03:00
| [Builder ](/creational/builder.md ) | Builds a complex object using simple objects | ✔ |
| [Factory Method ](/creational/factory.md ) | Defers instantiation of an object to a specialized function for creating instances | ✔ |
| [Object Pool ](/creational/object-pool.md ) | Instantiates and maintains a group of objects instances of the same type | ✔ |
| [Singleton ](/creational/singleton.md ) | Restricts instantiation of a type to one object | ✔ |
2015-12-15 01:31:23 +03:00
2016-09-13 14:42:18 +03:00
## Structural Patterns
2015-12-15 01:31:23 +03:00
2016-09-13 15:41:40 +03:00
| Pattern | Description | Status |
|:-------:|:----------- |:------:|
2020-05-02 20:40:25 +03:00
| [Bridge ](/structural/bridge/main.go ) | Decouples an interface from its implementation so that the two can vary independently | ✔ |
| [Composite ](/structural/composite/main.go ) | Encapsulates and provides access to a number of different objects | ✔ |
2020-05-08 12:07:20 +03:00
| [Decorator ](/structural/decorator/decorator.md ) | Adds behavior to an object, statically or dynamically | ✔ |
2020-05-02 20:40:25 +03:00
| [Facade ](/structural/facade/main.go ) | Uses one type as an API to a number of others | ✔ |
| [Flyweight ](/structural/flyweight/main.go ) | Reuses existing instances of objects with similar/identical state to minimize resource usage | ✔ |
2020-05-08 12:07:20 +03:00
| [Proxy ](/structural/decorator/proxy.md ) | Provides a surrogate for an object to control it's actions | ✔ |
2021-08-21 03:35:48 +03:00
| [Adapter ](/structural/adapter/adapter.md ) | Provides a surrogate for an object to control it's actions | ✔ |
2015-12-15 01:31:23 +03:00
2016-09-13 14:42:18 +03:00
## Behavioral Patterns
2015-12-15 01:31:23 +03:00
2016-09-13 15:41:40 +03:00
| Pattern | Description | Status |
|:-------:|:----------- |:------:|
2020-05-02 20:40:25 +03:00
| [Chain of Responsibility ](/behavioral/chain_of_responsibility/main.go ) | Avoids coupling a sender to receiver by giving more than object a chance to handle the request | ✔ |
| [Command ](/behavioral/command/main.go ) | Bundles a command and arguments to call later | ✔ |
| [Mediator ](/behavioral/mediator/main.go ) | Connects objects and acts as a proxy | ✔ |
| [Memento ](/behavioral/memento/main.go ) | Generate an opaque token that can be used to go back to a previous state | ✔ |
| [Observer ](/behavioral/observer.md ) | Provide a callback for notification of events/changes to data | ✔ |
2020-05-11 16:26:26 +03:00
| [Registry ](/behavioral/registry.md ) | Keep track of all subclasses of a given class | ✔ |
2020-05-02 20:40:25 +03:00
| [State ](/behavioral/state/main.go ) | Encapsulates varying behavior for the same object based on its internal state | ✔ |
| [Strategy ](/behavioral/strategy.md ) | Enables an algorithm's behavior to be selected at runtime | ✔ |
| [Template ](/behavioral/template/main.go ) | Defines a skeleton class which defers some methods to subclasses | ✔ |
| [Visitor ](/behavioral/visitor/main.go ) | Separates an algorithm from an object on which it operates | ✔ |
2020-05-11 16:19:47 +03:00
| [Interpreter ](/behavioral/interpreter/interpreter.md ) | interpret your own language or composed commands | ✔ |
2020-05-06 12:24:11 +03:00
2016-09-13 14:42:18 +03:00
## Synchronization Patterns
2016-01-01 22:06:10 +03:00
2016-09-13 15:41:40 +03:00
| Pattern | Description | Status |
|:-------:|:----------- |:------:|
2020-05-02 20:40:25 +03:00
| [Condition Variable ](/synchronization/condition_variable.md ) | Provides a mechanism for threads to temporarily give up access in order to wait for some condition | ✘ |
| [Lock/Mutex ](/synchronization/mutex.md ) | Enforces mutual exclusion limit on a resource to gain exclusive access | ✘ |
| [Monitor ](/synchronization/monitor.md ) | Combination of mutex and condition variable patterns | ✘ |
| [Read-Write Lock ](/synchronization/read_write_lock.md ) | Allows parallel read access, but only exclusive access on write operations to a resource | ✘ |
| [Semaphore ](/synchronization/semaphore.md ) | Allows controlling access to a common resource | ✔ |
2016-01-01 22:06:10 +03:00
2016-09-13 14:42:18 +03:00
## Concurrency Patterns
2015-12-23 16:46:52 +03:00
2016-09-13 15:41:40 +03:00
| Pattern | Description | Status |
|:-------:|:----------- |:------:|
2020-05-02 20:40:25 +03:00
| [N-Barrier ](/concurrency/n_barrier/main.go ) | Prevents a process from proceeding until all N processes reach to the barrier | ✔ |
| [Bounded Parallelism ](/concurrency/bounded_parallelism.md ) | Completes large number of independent tasks with resource limits | ✔ |
| [Broadcast ](/concurrency/broadcast.md ) | Transfers a message to all recipients simultaneously | ✘ |
| [Coroutines ](/concurrency/coroutine.md ) | Subroutines that allow suspending and resuming execution at certain locations | ✘ |
| [Generators ](/concurrency/generator.md ) | Yields a sequence of values one at a time | ✔ |
2020-05-06 12:24:11 +03:00
| [Reactor ](/concurrency/reactor.md ) | Demultiplexes service requests delivered concurrently to a service handler and dispatches them synchronously to the associated request handlers | ✘ |
2020-05-02 20:40:25 +03:00
| [Parallelism ](/concurrency/parallelism.md ) | Completes large number of independent tasks | ✔ |
2020-05-09 11:20:57 +03:00
| [Producer Consumer ](/channel/producer_consumer ) | Separates tasks from task executions | ✔ |
2016-01-19 09:49:13 +03:00
2016-09-13 14:42:18 +03:00
## Messaging Patterns
2016-03-07 11:51:42 +03:00
2016-09-13 15:41:40 +03:00
| Pattern | Description | Status |
|:-------:|:----------- |:------:|
2020-05-02 20:40:25 +03:00
| [Fan-In ](/messaging/fan_in.md ) | Funnels tasks to a work sink (e.g. server) | ✔ |
| [Fan-Out ](/messaging/fan_out.md ) | Distributes tasks among workers (e.g. producer) | ✔ |
2020-05-08 12:19:36 +03:00
| [Futures & Promises ](/messaging/futures_promises.md ) | Acts as a place-holder of a result that is initially unknown for synchronization purposes | ✔ |
2020-05-02 20:40:25 +03:00
| [Publish/Subscribe ](/messaging/publish_subscribe.md ) | Passes information to a collection of recipients who subscribed to a topic | ✔ |
| [Push & Pull ](/messaging/push_pull.md ) | Distributes messages to multiple workers, arranged in a pipeline | ✘ |
2016-01-19 09:49:13 +03:00
2016-09-13 14:42:18 +03:00
## Stability Patterns
2016-01-01 22:06:10 +03:00
2016-09-13 15:41:40 +03:00
| Pattern | Description | Status |
|:-------:|:----------- |:------:|
2020-05-02 20:40:25 +03:00
| [Bulkheads ](/stability/bulkhead.md ) | Enforces a principle of failure containment (i.e. prevents cascading failures) | ✘ |
| [Circuit-Breaker ](/stability/circuit-breaker.md ) | Stops the flow of the requests when requests are likely to fail | ✔ |
2020-05-06 12:24:11 +03:00
| [Deadline ](/stability/deadline.md ) | 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) | ✔ |
2020-05-02 20:40:25 +03:00
| [Fail-Fast ](/stability/fail_fast.md ) | Checks the availability of required resources at the start of a request and fails if the requirements are not satisfied | ✘ |
| [Handshaking ](/stability/handshaking.md ) | Asks a component if it can take any more load, if it can't, the request is declined | ✘ |
| [Steady-State ](/stability/steady_state.md ) | For every service that accumulates a resource, some other service must recycle that resource | ✘ |
2016-01-01 22:06:10 +03:00
2016-09-13 14:42:18 +03:00
## Profiling Patterns
2016-01-01 22:06:10 +03:00
2016-09-13 15:41:40 +03:00
| Pattern | Description | Status |
|:-------:|:----------- |:------:|
2020-05-02 20:40:25 +03:00
| [Timing Functions ](/profiling/timing.md ) | Wraps a function and logs the execution | ✔ |
2016-01-01 22:06:10 +03:00
2016-09-13 14:42:18 +03:00
## Idioms
2016-01-01 22:06:10 +03:00
2016-09-13 15:41:40 +03:00
| Pattern | Description | Status |
|:-------:|:----------- |:------:|
2020-05-02 20:40:25 +03:00
| [Functional Options ](/idiom/functional-options.md ) | Allows creating clean APIs with sane defaults and idiomatic overrides | ✔ |
2016-01-01 22:06:10 +03:00
2016-09-13 14:42:18 +03:00
## Anti-Patterns
2016-01-01 22:06:10 +03:00
2016-09-13 15:41:40 +03:00
| Pattern | Description | Status |
|:-------:|:----------- |:------:|
2020-05-02 20:40:25 +03:00
| [Cascading Failures ](/anti-patterns/cascading_failures.md ) | A failure in a system of interconnected parts in which the failure of a part causes a domino effect | ✘ |
2020-05-06 12:14:54 +03:00
## A pattern implementation
2020-06-19 11:07:50 +03:00
[go-pattern-examples ](https://github.com/crazybber/go-pattern-examples )