mirror of
https://github.com/tmrts/go-patterns.git
synced 2024-11-22 04:56:09 +03:00
Include more patterns and a logo
This commit is contained in:
parent
6257c096bb
commit
b10a4f9cac
121
README.md
121
README.md
@ -1,63 +1,116 @@
|
|||||||
# Go Patterns [![Awesome Widget]][Awesome] [![Travis Widget]][Travis] [![License Widget]][License]
|
<p align="center">
|
||||||
|
<img src="/gopher.jpg" height="400">
|
||||||
|
</p>
|
||||||
|
|
||||||
|
# Go Patterns [![Travis Widget]][Travis] [![Awesome Widget]][Awesome] [![License Widget]][License]
|
||||||
[Awesome Widget]: https://img.shields.io/badge/awesome-%E2%9C%93-ff69b4.svg?style=flat-square
|
[Awesome Widget]: https://img.shields.io/badge/awesome-%E2%9C%93-ff69b4.svg?style=flat-square
|
||||||
[Awesome]: https://github.com/sindresorhus/awesome
|
[Awesome]: https://github.com/sindresorhus/awesome
|
||||||
[Travis Widget]: https://img.shields.io/travis/tmrts/awesome-google-cloud.svg?style=flat-square
|
[Travis Widget]: https://img.shields.io/travis/tmrts/awesome-google-cloud.svg?style=flat-square
|
||||||
[Travis]: http://travis-ci.org/tmrts/go-patterns
|
[Travis]: http://travis-ci.org/tmrts/go-patterns
|
||||||
[License Widget]: https://img.shields.io/badge/license-Creative%20Commons%204.0-E91E63.svg?style=flat-square
|
[License Widget]: https://img.shields.io/badge/license-Creative%20Commons%204.0-E91E63.svg?style=flat-square
|
||||||
[License]: http://creativecommons.org/licenses/by/4.0/
|
[License]: http://creativecommons.org/licenses/by/4.0/
|
||||||
A curated collection of common patterns & idioms for Go.
|
A curated collection of idiomatic design & application patterns for Go language.
|
||||||
|
|
||||||
__Creational Patterns__:
|
__Creational Patterns__:
|
||||||
|
|
||||||
| Pattern | Description |
|
| Pattern | Description |
|
||||||
|:-------:| ----------- |
|
|:-------:| ----------- |
|
||||||
| [abstract_factory](abstract_factory.go) | use a generic function with specific factories |
|
| [Abstract Factory](abstract_factory.go) | use a generic function with specific factories |
|
||||||
| [borg](borg.go) | a singleton with shared-state among instances |
|
| [Borg Singleton](borg_singleton.go) | a singleton with shared-state among instances |
|
||||||
| [builder](builder.go) | instead of using multiple constructors, builder object receives parameters and returns constructed objects |
|
| [Builder](builder.go) | instead of using multiple constructors, builder object receives parameters and returns constructed objects |
|
||||||
| [factory_method](factory_method.go) | delegate a specialized function/method to create instances |
|
| [Factory Method](factory_method.go) | delegate a specialized function/method to create instances |
|
||||||
| [lazy_evaluation](lazy_evaluation.go) | lazily-evaluated property pattern in Go |
|
| [Lazy Evaluation](lazy_evaluation.go) | lazily-evaluated property pattern in Go |
|
||||||
| [pool](pool.go) | preinstantiate and maintain a group of instances of the same type |
|
| [Object Pool](object_pool.go) | preinstantiate and maintain a group of instances of the same type |
|
||||||
| [prototype](prototype.go) | use a factory and clones of a prototype for new instances (if instantiation is expensive) |
|
| [Prototype](prototype.go) | use a factory and clones of a prototype for new instances (if instantiation is expensive) |
|
||||||
|
|
||||||
__Structural Patterns__:
|
__Structural Patterns__:
|
||||||
|
|
||||||
| Pattern | Description |
|
| Pattern | Description |
|
||||||
|:-------:| ----------- |
|
|:-------:| ----------- |
|
||||||
| [3-tier](3-tier.go) | data<->business logic<->presentation separation (strict relationships) |
|
| [Adapter](adapter.go) | adapt one interface to another using a white-list |
|
||||||
| [adapter](adapter.go) | adapt one interface to another using a white-list |
|
| [Bridge](bridge.go) | a client-provider middleman to soften interface changes |
|
||||||
| [bridge](bridge.go) | a client-provider middleman to soften interface changes |
|
| [Composite](composite.go) | encapsulate and provide access to a number of different objects |
|
||||||
| [composite](composite.go) | encapsulate and provide access to a number of different objects |
|
| [Decorator](decorator.go) | Adds behavior to an object, statically or dynamically |
|
||||||
| [decorator](decorator.go) | wrap functionality with other functionality in order to affect outputs |
|
| [Facade](facade.go) | use one class as an API to a number of others |
|
||||||
| [facade](facade.go) | use one class as an API to a number of others |
|
| [Flyweight](flyweight.go) | transparently reuse existing instances of objects with similar/identical state |
|
||||||
| [flyweight](flyweight.go) | transparently reuse existing instances of objects with similar/identical state |
|
| [Model View Controller](mvc.go) | model<->view<->controller (non-strict relationships) |
|
||||||
| [front_controller](front_controller.go) | single handler requests coming to the application |
|
| [Proxy](proxy.go) | an object funnels operations to something else |
|
||||||
| [mvc](mvc.go) | model<->view<->controller (non-strict relationships) |
|
|
||||||
| [proxy](proxy.go) | an object funnels operations to something else |
|
|
||||||
|
|
||||||
__Behavioral Patterns__:
|
__Behavioral Patterns__:
|
||||||
|
|
||||||
| Pattern | Description |
|
| Pattern | Description |
|
||||||
|:-------:| ----------- |
|
|:-------:| ----------- |
|
||||||
| [chain](chain.go) | apply a chain of successive handlers to try and process the data |
|
| [Chain](chain.go) | apply a chain of successive handlers to try and process the data |
|
||||||
| [catalog](catalog.go) | general methods will call different specialized methods based on construction parameter |
|
| [Catalog](catalog.go) | general methods will call different specialized methods based on construction parameter |
|
||||||
| [chaining_method](chaining_method.go) | continue callback next object method |
|
| [Chaining Method](chaining_method.go) | continue callback next object method |
|
||||||
| [command](command.go) | bundle a command and arguments to call later |
|
| [Command](command.go) | bundle a command and arguments to call later |
|
||||||
| [mediator](mediator.go) | an object that knows how to connect other objects and act as a proxy |
|
| [Mediator](mediator.go) | an object that knows how to connect other objects and act as a proxy |
|
||||||
| [memento](memento.go) | generate an opaque token that can be used to go back to a previous state |
|
| [Memento](memento.go) | generate an opaque token that can be used to go back to a previous state |
|
||||||
| [observer](observer.go) | provide a callback for notification of events/changes to data |
|
| [Observer](observer.go) | provide a callback for notification of events/changes to data |
|
||||||
| [publish_subscribe](publish_subscribe.go) | a source syndicates events/data to 0+ registered listeners |
|
| [Publish/Subscribe](publish_subscribe.go) | Passes information to a collection of recipients who subscribed to a topic |
|
||||||
| [registry](registry.go) | keep track of all subclasses of a given class |
|
| [Registry](registry.go) | keep track of all subclasses of a given class |
|
||||||
| [specification](specification.go) | business rules can be recombined by chaining the business rules together using boolean logic |
|
| [Specification](specification.go) | business rules can be recombined by chaining the business rules together using boolean logic |
|
||||||
| [state](state.go) | logic is organized into a discrete number of potential states and the next state that can be transitioned to |
|
| [State](state.go) | logic is organized into a discrete number of potential states and the next state that can be transitioned to |
|
||||||
| [strategy](strategy.go) | selectable operations over the same data |
|
| [Strategy](strategy.go) | Encapsulates an algorithm inside a struct |
|
||||||
| [template](template.go) | an object imposes a structure but takes pluggable components |
|
| [Template](template.go) | an object imposes a structure but takes pluggable components |
|
||||||
| [visitor](visitor.go) | invoke a callback for all items of a collection |
|
| [Visitor](visitor.go) | invoke a callback for all items of a collection |
|
||||||
|
|
||||||
|
__Synchronization Patterns__:
|
||||||
|
|
||||||
|
| Pattern | Description |
|
||||||
|
|:-------:| ----------- |
|
||||||
|
| [Lock/Mutex](mutex.go) | Enforces mutual exclusion limit on accessing a resource |
|
||||||
|
| [Read-Write Lock](read_write_lock.go) | |
|
||||||
|
| [Condition Variable](condition_variable.go) | |
|
||||||
|
| [Monitor](monitor.go) | Combination of mutex and condition patterns |
|
||||||
|
| [Semaphore](semaphore.go) | Allows controlling access to a common resource |
|
||||||
|
|
||||||
__Concurrency Patterns__:
|
__Concurrency Patterns__:
|
||||||
|
|
||||||
| Pattern | Description |
|
| Pattern | Description |
|
||||||
|:-------:| ----------- |
|
|:-------:| ----------- |
|
||||||
| [semaphore](semaphore.go) | data type for controlling access, to a common resource|
|
| [Scheduler](scheduler.go) | |
|
||||||
|
| [Barrier](barrier.go) | |
|
||||||
|
| [Producer Consumer](producer_consumer.go) | |
|
||||||
|
| [Futures](future.go) | |
|
||||||
|
| [Broadcast](broadcast.go) | |
|
||||||
|
| [Multiplex](multiplex.go) | |
|
||||||
|
| [Fan-In](fan_in.go) | |
|
||||||
|
| [Generators](generator.go) | |
|
||||||
|
| [Coroutines](coroutine.go) | |
|
||||||
|
|
||||||
|
__Stability Patterns__:
|
||||||
|
|
||||||
|
| Pattern | Description |
|
||||||
|
|:-------:| ----------- |
|
||||||
|
| [Bulkheads](bulkhead.go) | |
|
||||||
|
| [Circuit Breaker](circuit_breaker.go) | |
|
||||||
|
| [Deadline](deadline.go) | |
|
||||||
|
| [Fail Fast](fail_fast.go) | |
|
||||||
|
| [Handshaking](handshaking.go) | |
|
||||||
|
| [Steady State](steady_state.go) | |
|
||||||
|
|
||||||
|
__Profiling Patterns__:
|
||||||
|
|
||||||
|
| Pattern | Description |
|
||||||
|
|:-------:| ----------- |
|
||||||
|
| [Timing Functions](timing.go) | Wraps a function and logs the execution |
|
||||||
|
|
||||||
|
__Idioms__:
|
||||||
|
|
||||||
|
| Pattern | Description |
|
||||||
|
|:-------:| ----------- |
|
||||||
|
| [Functional Options](functional_options.go) | Allows creating clean APIs with sane defaults and idiomatic overrides |
|
||||||
|
|
||||||
|
__Anti-Patterns__:
|
||||||
|
|
||||||
|
| Pattern | Description |
|
||||||
|
|:-------:| ----------- |
|
||||||
|
|
||||||
|
__Other Patterns__:
|
||||||
|
|
||||||
|
| Pattern | Description |
|
||||||
|
|:-------:| ----------- |
|
||||||
|
|
||||||
# License
|
# License
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import "os"
|
import "os"
|
||||||
|
|
||||||
type arguments struct {
|
type Arguments struct {
|
||||||
UID int
|
UID int
|
||||||
GID int
|
GID int
|
||||||
Flags int
|
Flags int
|
||||||
@ -10,35 +10,35 @@ type arguments struct {
|
|||||||
Permissions os.FileMode
|
Permissions os.FileMode
|
||||||
}
|
}
|
||||||
|
|
||||||
type argument func(*arguments)
|
type Argument func(*Arguments)
|
||||||
|
|
||||||
func UID(userID int) argument {
|
func UID(userID int) Argument {
|
||||||
return func(args *arguments) {
|
return func(args *Arguments) {
|
||||||
args.UID = userID
|
args.UID = userID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GID(groupID int) argument {
|
func GID(groupID int) Argument {
|
||||||
return func(args *arguments) {
|
return func(args *Arguments) {
|
||||||
args.GID = groupID
|
args.GID = groupID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Contents(c string) argument {
|
func Contents(c string) Argument {
|
||||||
return func(args *arguments) {
|
return func(args *Arguments) {
|
||||||
args.Contents = c
|
args.Contents = c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Permissions(perms os.FileMode) argument {
|
func Permissions(perms os.FileMode) Argument {
|
||||||
return func(args *arguments) {
|
return func(args *Arguments) {
|
||||||
args.Permissions = perms
|
args.Permissions = perms
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(filepath string, setters ...argument) error {
|
func New(filepath string, setters ...Argument) error {
|
||||||
// Default arguments
|
// Default Arguments
|
||||||
args := &arguments{
|
args := &Arguments{
|
||||||
UID: os.Getuid(),
|
UID: os.Getuid(),
|
||||||
GID: os.Getgid(),
|
GID: os.Getgid(),
|
||||||
Contents: "",
|
Contents: "",
|
||||||
|
BIN
gopher.jpg
Normal file
BIN
gopher.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 158 KiB |
62
publish_subscribe.go
Normal file
62
publish_subscribe.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrTopicClosed = errors.New("Topic has been closed")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Message
|
||||||
|
type Message string
|
||||||
|
|
||||||
|
// Topic
|
||||||
|
type Topic struct {
|
||||||
|
Subscribers []Authentication
|
||||||
|
MessageHistory []struct {
|
||||||
|
Author string
|
||||||
|
Message Message
|
||||||
|
Timestamp time.Time
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscribe
|
||||||
|
func (t *Topic) Subscribe(Authentication) (Subscription, error) {
|
||||||
|
// Implementation
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unsubscribe
|
||||||
|
func (t *Topic) Unsubscribe(Subscription) error {
|
||||||
|
// Implementation
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete
|
||||||
|
func (t *Topic) Delete() error {
|
||||||
|
// Implementation
|
||||||
|
}
|
||||||
|
|
||||||
|
type Subscription struct {
|
||||||
|
ch chan<- Message
|
||||||
|
|
||||||
|
Inbox chan Message
|
||||||
|
}
|
||||||
|
|
||||||
|
// Publish
|
||||||
|
func (s *Subscription) Publish(msg Message) error {
|
||||||
|
if _, ok := ch; !ok {
|
||||||
|
return ErrTopicClosed
|
||||||
|
}
|
||||||
|
|
||||||
|
ch <- msg
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Authentication
|
||||||
|
type Authentication struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user