Top Related Projects
Standard Go Project Layout
A curated list of awesome Go frameworks, libraries and software
ā¤ļø 1000+ Hand-Crafted Go Examples, Exercises, and Quizzes. š Learn Go by fixing 1000+ tiny programs.
Learn Go with test-driven development
A standard library for microservices.
ā”ļø Express inspired web framework written in Go
Quick Overview
The tmrts/go-patterns repository is a curated collection of idiomatic design and application patterns for Go programming language. It serves as a comprehensive resource for developers to learn and implement best practices in Go, covering various aspects of software design and development.
Pros
- Extensive coverage of design patterns and idioms specific to Go
- Well-organized and categorized patterns for easy reference
- Includes code examples and explanations for each pattern
- Regularly updated and maintained by the community
Cons
- Some patterns may be too advanced for beginners
- Not all patterns are applicable to every project or use case
- Lacks in-depth explanations for some complex patterns
- May require additional research to fully understand and implement certain patterns
Code Examples
- Creational Pattern: Singleton
type singleton struct {}
var instance *singleton
var once sync.Once
func GetInstance() *singleton {
once.Do(func() {
instance = &singleton{}
})
return instance
}
This example demonstrates the Singleton pattern, ensuring only one instance of a struct is created.
- Behavioral Pattern: Observer
type Observer interface {
Update(string)
}
type Subject struct {
observers []Observer
state string
}
func (s *Subject) Attach(o Observer) {
s.observers = append(s.observers, o)
}
func (s *Subject) Notify() {
for _, observer := range s.observers {
observer.Update(s.state)
}
}
This code shows the Observer pattern, allowing multiple objects to watch and react to changes in another object.
- Concurrency Pattern: Worker Pool
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
fmt.Printf("worker %d started job %d\n", id, j)
time.Sleep(time.Second)
fmt.Printf("worker %d finished job %d\n", id, j)
results <- j * 2
}
}
func main() {
jobs := make(chan int, 100)
results := make(chan int, 100)
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
for a := 1; a <= 5; a++ {
<-results
}
}
This example illustrates the Worker Pool pattern, demonstrating how to manage and distribute tasks among multiple goroutines.
Getting Started
To use the patterns from this repository:
-
Clone the repository:
git clone https://github.com/tmrts/go-patterns.git
-
Browse the patterns in the repository's directories.
-
Study the pattern descriptions and code examples.
-
Implement the desired pattern in your Go project, adapting it to your specific needs.
-
Refer to the repository's README for additional information and resources.
Competitor Comparisons
Standard Go Project Layout
Pros of project-layout
- Provides a comprehensive directory structure for Go projects
- Includes explanations for each directory's purpose
- Offers a standardized approach to organizing Go applications
Cons of project-layout
- May be overly complex for smaller projects
- Some developers argue it doesn't follow Go's simplicity principle
- Can lead to unnecessary boilerplate in certain cases
Code Comparison
project-layout example structure:
āāā cmd/
ā āāā myapp/
āāā internal/
ā āāā pkg/
āāā pkg/
āāā vendor/
āāā api/
go-patterns example (Design Pattern):
type Strategy interface {
Execute()
}
type Context struct {
strategy Strategy
}
func (c *Context) SetStrategy(strategy Strategy) {
c.strategy = strategy
}
Key Differences
- go-patterns focuses on design patterns and idiomatic Go code examples
- project-layout emphasizes project structure and organization
- go-patterns is more educational, while project-layout is a practical guide for project setup
Use Cases
- Use go-patterns to learn about Go design patterns and best practices
- Use project-layout as a starting point for structuring large Go applications
Community and Maintenance
- Both repositories are popular and well-maintained
- project-layout has more contributors and is updated more frequently
- go-patterns has a larger number of stars, indicating high interest in design patterns
A curated list of awesome Go frameworks, libraries and software
Pros of awesome-go
- Comprehensive collection of Go resources, libraries, and tools
- Regularly updated with community contributions
- Well-organized into categories for easy navigation
Cons of awesome-go
- Lacks in-depth explanations of design patterns
- May overwhelm beginners with the sheer volume of information
Code comparison
go-patterns provides specific code examples for design patterns:
type Strategy interface {
Execute()
}
type Context struct {
strategy Strategy
}
func (c *Context) SetStrategy(strategy Strategy) {
c.strategy = strategy
}
awesome-go doesn't typically include code snippets, focusing instead on linking to external resources:
## Database
*Databases implemented in Go.*
* [BadgerDB](https://github.com/dgraph-io/badger) - Fast key-value DB in Go.
* [BigCache](https://github.com/allegro/bigcache) - Efficient key/value cache for gigabytes of data.
Summary
go-patterns focuses on explaining Go design patterns with code examples, while awesome-go serves as a comprehensive directory of Go-related resources. go-patterns is more suitable for developers looking to understand and implement specific patterns, whereas awesome-go is an excellent starting point for discovering a wide range of Go tools and libraries.
ā¤ļø 1000+ Hand-Crafted Go Examples, Exercises, and Quizzes. š Learn Go by fixing 1000+ tiny programs.
Pros of learngo
- Comprehensive learning resource with structured tutorials and exercises
- Regularly updated with new content and improvements
- Includes practical examples and hands-on projects
Cons of learngo
- Primarily focused on learning, less suitable for quick reference
- May not cover advanced design patterns in depth
Code Comparison
go-patterns example (Singleton pattern):
type singleton struct {}
var instance *singleton
var once sync.Once
func GetInstance() *singleton {
once.Do(func() {
instance = &singleton{}
})
return instance
}
learngo example (Basic variable declaration):
package main
import "fmt"
func main() {
var speed int
fmt.Println(speed)
}
The go-patterns repository focuses on design patterns and idiomatic Go code, providing concise examples of various patterns. In contrast, learngo is a more extensive learning resource, starting from basic concepts and progressing to more advanced topics.
go-patterns is better suited for experienced developers looking for quick references to design patterns, while learngo is ideal for beginners and those seeking a structured learning path in Go programming.
Both repositories offer valuable resources for Go developers, but they serve different purposes and target audiences. The choice between them depends on the user's experience level and specific learning or reference needs.
Learn Go with test-driven development
Pros of learn-go-with-tests
- Focuses on test-driven development (TDD) approach
- Provides hands-on learning experience with practical examples
- Covers a wide range of Go concepts and features
Cons of learn-go-with-tests
- May be less comprehensive in covering design patterns
- Requires more time investment to work through examples
- Might be challenging for absolute beginners in programming
Code Comparison
learn-go-with-tests example:
func TestHello(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
go-patterns example:
type Observer interface {
Update(subject *Subject)
}
type Subject struct {
observers []Observer
state int
}
Summary
learn-go-with-tests is ideal for those who want to learn Go through practical, test-driven development. It offers a hands-on approach but may require more time investment. go-patterns, on the other hand, focuses on design patterns and might be more suitable for experienced developers looking to improve their Go code structure. The choice between the two depends on the learner's goals and preferred learning style.
A standard library for microservices.
Pros of kit
- Comprehensive microservices toolkit with production-ready components
- Active development and community support
- Extensive documentation and examples for real-world use cases
Cons of kit
- Steeper learning curve due to its complexity and extensive features
- May be overkill for smaller projects or simple applications
- Requires more setup and configuration compared to simpler pattern libraries
Code Comparison
go-patterns focuses on design patterns and idiomatic Go code:
// Observer pattern example
type Observer interface {
Update(string)
}
type Subject struct {
observers []Observer
state string
}
kit provides more complex, production-ready components:
// Endpoint definition example
func makeUppercaseEndpoint(svc StringService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(uppercaseRequest)
v, err := svc.Uppercase(req.S)
return uppercaseResponse{v, err}, nil
}
}
go-patterns is ideal for learning and understanding Go patterns, while kit is better suited for building robust microservices in production environments.
ā”ļø Express inspired web framework written in Go
Pros of Fiber
- Actively maintained and regularly updated web framework
- High-performance and low memory footprint
- Extensive documentation and large community support
Cons of Fiber
- Focused solely on web development, not a general-purpose pattern collection
- Steeper learning curve for beginners compared to simple pattern examples
- May be overkill for small projects or learning basic Go concepts
Code Comparison
go-patterns (Singleton pattern):
type singleton struct {}
var instance *singleton
var once sync.Once
func GetInstance() *singleton {
once.Do(func() {
instance = &singleton{}
})
return instance
}
Fiber (Basic HTTP server):
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Listen(":3000")
Summary
go-patterns is a collection of design patterns and idioms in Go, serving as a reference for various programming concepts. Fiber, on the other hand, is a full-fledged web framework focused on building high-performance web applications. While go-patterns is excellent for learning and understanding Go patterns, Fiber provides a complete toolkit for web development with features like routing, middleware, and templating.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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 | Ć¢ĀĀ |
Top Related Projects
Standard Go Project Layout
A curated list of awesome Go frameworks, libraries and software
ā¤ļø 1000+ Hand-Crafted Go Examples, Exercises, and Quizzes. š Learn Go by fixing 1000+ tiny programs.
Learn Go with test-driven development
A standard library for microservices.
ā”ļø Express inspired web framework written in Go
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot