Skip to content
/ go-clean-starter Public template

Go clean architecture template to boost up development πŸš€ Suitable from small to big teams!

License

Notifications You must be signed in to change notification settings

SoraDaibu/go-clean-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

go-clean-starter

πŸ‡―πŸ‡΅ ζ—₯本θͺž

This is a Go template of clean architecture to ease and boost up a backend development with up to date libraries utilizing clean architecture as design.

Requirements

Name Used for
Go for backend application
Docker and docker-compose for containers
goimports for formatting source code
golangci-lint for linting source code

Overview

This app has three containers.

  • api to run api server
  • task to run task
  • postgres for database

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           Docker Compose Project           β”‚
β”‚                                            β”‚
β”‚    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚    β”‚     API      β”‚   β”‚   Task-Runner  β”‚   β”‚
β”‚    β”‚    (HTTP)    β”‚   β”‚   (CLI Tasks)  β”‚   β”‚
β”‚    β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚            β”‚                  β”‚            β”‚
β”‚            β–Ό                  β–Ό            β”‚
β”‚     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚     β”‚          PostgreSQL DB         β”‚     β”‚
β”‚     β”‚     (go_clean_starter DB)      β”‚     β”‚
β”‚     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Technologies

  • Go: Primary language for high performance, simplicity, and strong concurrency support
  • Echo: Fast and lightweight HTTP web framework with clean routing and rich middleware support
  • PostgreSQL: Reliable database with advanced features like JSONB, full-text search, and strong consistency
  • sqlc: Generates type-safe Go code from raw SQL queries for compile-time safety and better performance
  • golang-migrate: Handles database schema migrations with version control, locking, and rollback support
  • oapi-codegen: Generates Go structs and types from OpenAPI specifications for type-safe API development
  • Wire: Compile-time dependency injection for clean, type-safe, and maintainable object initialization
  • Air: Hot reload tool for fast development cycles with automatic rebuilds on file changes
  • Docker & Docker Compose: Containerization for consistent development and deployment environments

Directory structure

go-clean-starter
β”œβ”€β”€ builder
β”‚   β”œβ”€β”€ dependency.go
β”‚   β”œβ”€β”€ wire.go         # manually written wire code
β”‚   └── wire_gen.go     # wire code auto generated by `wire.go`
β”œβ”€β”€ cmd
β”‚   β”œβ”€β”€ migration.go    # command to run migration
β”‚   β”œβ”€β”€ serve.go        # command to run API server
β”‚   └── task.go         # commands to run tasks (currently only one task is defined here)
β”œβ”€β”€ config
β”œβ”€β”€ domain # domain models
β”œβ”€β”€ go.sum
β”œβ”€β”€ internal
β”‚   β”œβ”€β”€ http # http layer
β”‚   β”‚   β”œβ”€β”€ base
β”‚   β”‚   β”œβ”€β”€ handler
β”‚   β”‚   β”‚   β”œβ”€β”€ errors.go
β”‚   β”‚   β”‚   β”œβ”€β”€ openapi_types.gen.go # auto generated Go structs by doc/api.yaml
β”‚   β”‚   β”‚   └── user
β”‚   β”‚   β”œβ”€β”€ middleware
β”‚   β”‚   └── server.go
β”‚   β”œβ”€β”€ repository # data access layer
β”‚   β”‚   β”œβ”€β”€ item
β”‚   β”‚   β”œβ”€β”€ user
β”‚   β”‚   └── transaction.go
β”‚   β”œβ”€β”€ service # business logic layer
β”‚   β”‚   └── user
β”‚   β”œβ”€β”€ sqlc # sqlc input & output
β”‚   β”‚   β”œβ”€β”€ query
β”‚   β”‚   β”‚   β”œβ”€β”€ items.sql
β”‚   β”‚   β”‚   └── users.sql
β”‚   β”‚   β”œβ”€β”€ db.go
β”‚   β”‚   β”œβ”€β”€ items.sql.go
β”‚   β”‚   β”œβ”€β”€ models.go
β”‚   β”‚   └── users.sql.go
β”‚   └── task # task program such as job, batch, etc...
β”‚       └── item
β”‚           β”œβ”€β”€ data
β”‚           β”‚   └── item.csv
β”‚           β”œβ”€β”€ item.go
β”‚           └── usecase.go
β”œβ”€β”€ main.go
β”œβ”€β”€ migration
β”‚   β”œβ”€β”€ migrate.go
β”‚   └── sql # migration sql files
β”œβ”€β”€ pkg                         # pkg is empty now. add packages that can be imported by other projects
β”œβ”€β”€ README.md
β”œβ”€β”€ WHY.md                      # document to explain why this tech stack and tech specifications are choosen
β”œβ”€β”€ docker-compose.test.yaml
β”œβ”€β”€ docker-compose.yaml
└── sqlc.yaml                    # configuration of sqlc

Setup

Setup environment values

  • create .env by copying .env.sample
cp .env.sample .env

Docker

  • run make build to build Docker images
make build
  • run make up to create and run Docker containers by docker-compose
make up

πŸ’‘ This project uses air, which is Go hot reload. Hot reload helps us to develop faster.

Test

  • run make test to test make test will create a test environment using the test specific docker-compose.test.yaml with the same Dockerfile as development. Thus safety of test environment is assured as it's separated from local environment for development.
make test

API Usage

Once the server is running, you can interact with the API:

πŸ“‹ Complete API documentation is available in api.yaml - OpenAPI 3.1 specification with all endpoints, schemas, and examples.

Health Check

curl http://localhost:8080/health

Getting Help

  • Check Issues for known problems
  • Review WHY.md for architecture decisions

Contributions

read CONTRIUTING.md to contribute to this project.

About

Go clean architecture template to boost up development πŸš€ Suitable from small to big teams!

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages