/go-patterns

A curated list of Go patterns and idioms

Primary LanguageGoApache License 2.0Apache-2.0

Go Patterns Travis Widget Awesome Widget License Widget

A curated collection of idiomatic design & application patterns for Go language.

Creational Patterns:

Pattern Description
TODO: Abstract Factory Provides an interface for creating families of releated objects
TODO: Builder Builds a complex object using simple objects
TODO: 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
TODO: Adapter Adapts otherwise incompatible interfaces to work together by adapting one to the other
TODO: Bridge Decouples an interface from its implementation so that the two can vary independently
TODO: Composite Encapsulates and provides access to a number of different objects
Decorator Adds behavior to an object, statically or dynamically
TODO: Facade Uses one type as an API to a number of others
TODO: Flyweight Reuses existing instances of objects with similar/identical state to minimize resource usage
TODO: Model View Controller Divides an app into three interconnected parts to separate internal representation from presentation to user
TODO: Proxy Provides a surrogate for an object to control it's actions

Behavioral Patterns:

Pattern Description
TODO: Chain of Responsibility Avoids coupling a sender to receiver by giving more than object a chance to handle the request
TODO: Command Bundles a command and arguments to call later
TODO: Mediator Connects objects and acts as a proxy
TODO: 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
TODO: Registry Keep track of all subclasses of a given class
TODO: State Encapsulates varying behavior for the same object based on its internal state
Strategy Enables an algorithm's behavior to be selected at runtime
TODO: Template Defines a skeleton class which defers some methods to subclasses
TODO: Visitor Separates an algorithm from an object on which it operates

Synchronization Patterns:

Pattern Description
TODO: Condition Variable Provides a mechanism for threads to temporarily give up access in order to wait for some condition
TODO: Lock/Mutex Enforces mutual exclusion limit on a resource to gain exclusive access
TODO: Monitor Combination of mutex and condition variable patterns
TODO: 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
TODO: 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
TODO: Broadcast Transfers a message to all recipients simultaneously
TODO: Coroutines Subroutines that allow suspending and resuming execution at certain locations
TODO: Generators Yields a sequence of values one at a time
TODO: 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
TODO: Producer Consumer Separates tasks from task executions
TODO: Scheduler Orchestrates steps to be performed as part of a task

Messaging Patterns:

Pattern Description
Fan-In Funnels tasks to a work sink (e.g. server)
Fan-Out Distributes tasks among workers (e.g. producer)
TODO: 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
TODO: Push & Pull Distributes messages to multiple workers, arranged in a pipeline

Stability Patterns:

Pattern Description
TODO: 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
TODO: 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)
TODO: Fail-Fast Checks the availability of required resources at the start of a request and fails if the requirements are not satisfied
TODO: Handshaking Asks a component if it can take any more load, if it can't the request is declined
TODO: Steady-State For every service that accumulates a resource, some other service must recycle that resource

Profiling Patterns:

Pattern Description
TODO: Timing Functions Wraps a function and logs the execution

Idioms:

Pattern Description
Functional Options Allows creating clean APIs with sane defaults and idiomatic overrides

Anti-Patterns:

Pattern Description
TODO: Cascading Failures A failure in a system of interconnected parts in which the failure of a part causes a domino effect

Other Patterns:

Pattern Description

License

Creative Commons License

This work is licensed under a Creative Commons Attribution 4.0 International License.