Welcome to Pattern Garden, a collection of classic software design patterns implemented in Go. This repository serves as a practical guide and a reference for understanding and applying these patterns in real-world Go applications. It is also a personal project for learning and experimenting with Go.
Here are the design patterns currently available in this collection:
The Options pattern provides a clean and extensible way to handle optional parameters in constructors and other functions. It uses functional options to configure a struct, improving readability and maintainability over tangled constructors or zero-value checks.
Example:
The Server struct in patterngarden/patterns/options/server.go is configured using this pattern.
func Run() {
server1 := NewServer("api.example.com",
WithMaxConnections(20),
WithPort(9000),
WithTimeout(60*time.Second),
WithTLS(true),
)
The Repository pattern abstracts the data layer, providing a clean API for the application to interact with data sources without being tied to a specific implementation. This promotes separation of concerns and makes the application more testable and maintainable.
This project includes:
- A generic
UserRepositoryinterface. - An
InMemoryUserRepositoryfor testing and development. - A
PostgresUserRepositoryfor production use with a PostgreSQL database.
Example:
The UserService is initialized with a UserRepository implementation. You can easily switch between the in-memory and Postgres repositories.
fmt.Println("--- Running with Postgres Repository ---")
pgRepo := repository.NewPostgresUserRepository(db)
userService := repository.NewUserService(pgRepo)
// fmt.Println("--- Running with In-Memory Repository ---")
// memRepo := repository.NewInMemoryUserRepository()
// userService := repository.NewUserService(memRepo)
The Factory pattern provides an interface for creating objects in a superclass but lets subclasses alter the type of objects that will be created. The placeholder for this pattern can be found in patterngarden/patterns/factory/.
The Pipeline pattern is a design pattern that allows you to chain together a series of processing steps, each of which can modify or transform the input data. The placeholder for this pattern can be found in patterngarden/patterns/pipeline/.
Follow these instructions to get the project up and running on your local machine.
The recommended way to run the project is with Docker, which handles the entire environment setup.
-
Clone the repository:
git clone <repository-url> cd patterngarden
-
Start the services: Use the provided
Makefileto build and start the Go application and the PostgreSQL database in Docker containers.make docker-up
On the first run, Docker will automatically initialize the database and create the necessary tables using the
db/init/init.sqlscript. -
View Logs: To see the output from the running application and database, you can tail the logs.
make docker-logs
-
Stop the services: To stop the containers and remove the network, use:
make docker-down
If you prefer to run the Go application directly on your host machine, you can follow these steps:
-
Start the database: You still need Docker to run the PostgreSQL database.
docker-compose -f deploy/docker-compose.yml up -d db
-
Set up environment variables: The application requires a database connection string. You must set it as an environment variable to point to the Dockerized database.
export DB_CONNECTION_STRING="postgres://myuser:mypassword@localhost:5432/mydb?sslmode=disable"
-
Run the application: Use the
make runcommand or run themain.gofile directly.make run
patterngarden/
├── cmd/patterngarden/ # Main application entry point
├── db/init/ # SQL database initialization script
├── deploy/ # Docker and deployment configurations
├── patterns/ # Core design pattern implementations
│ ├── options/ # Options pattern
│ ├── repository/ # Repository pattern
│ └── factory/ # Factory pattern (WIP)
├── go.mod # Go module definitions
├── go.sum # Go module checksums
└── Makefile # Make commands for building, running, and managing Docker