A starter kit written in Golang + Ports and Adapters structure on top of https://github.com/golang-standards/project-layout
git clone [email protected]/yuraxdrumz/golang-starter-kit
go install github.com/golangci/golangci-lint/cmd/[email protected]
- Run
brew install pre-commit
/pip install pre-commit
- Run
pre-commit install
If you want to check pre-commit hook on current folder run pre-commit run --all-files
Ports and Adapters divides your code to 3 parts:
- Business-logic - These are your business rules + types, implemented without any dependency on 3rd party modules (self-contained)
- Ports - Interfaces to speak with your business rules
- Adapters - Implementations of the ports, There are two kinds of adapters:
- In(Driver) - your external API to the world. For example -
internal/pkg/adapters/in/http.go
- Out(Driven) - what your business logic uses. For example -
internal/pkg/adapters/out/reverser/in-memory.go
Usually, you divide ports
and adapters
to separate directories, but the best practice in golang is to keep structs near implementations. That is why, I decided to add ports.go
near each adapter.
Adding a new business logic:
- Create appropriate structs in
ports.go
file underinternal/app/your-use-case/ports.go
- Create your use-case with your application specific logic under
internal/app/your-use-case/logic.go
- Create your in/out adapter, for example -
Repository(out)
orgRPC(in)
.internal/pkg/adapters/*
- Tests!!!
your-file-name_test.go
under same directory as fileinternal/app/your-use-case/logic_test.go
go get -u <repository-name>
go run main.go
To read environement variables, this service uses envconfig
library, which allows defining a struct of environment variables. You can easily add new variables under internal/pkg/adapters/out/env
The default is:
type Specification struct {
LogLevel string `envconfig:"LOG_LEVEL" default:"info"`
Port string `envConfig:"PORT" default:"8080"`
}
By default this service looks for .env in root folder.
The repo uses logrus
logger to write to stdout.
To change log level change the LOG_LEVEL
environment variable
Possible log levels:
- info - default
- debug
- error
- warn
- fatal
You can add any pre commit hooks to .pre-commit-config.yaml
if needed