Skip to content

protiumx/template-go-service

Repository files navigation

template-go-service

Service oriented golang template with relational database. The goal of this template is to provide an initial setup for DB migrations and code generation, using Docker, Postgres, and a task runner.

Dependencies

Init

This template does not include the go module file. To create a new one and add dependencies run:

go mod init <package>
go mod tidy
go get ./...

Folder structure

  • cmd/: app entry points
  • db/:
    • migrations/: SQL migrations files
    • queries/: SQL query files used by sqlc
  • pkg/: app sources
    • db/: code generated by sqlc

Docker compose

Docker compose can be used to setup a db instance. Use

docker-compose up -d

Note: migrations will run once the postgres container is ready using docker-compose depend-on

Database

The schema.dbml file is the db schema source of truth. It is used to generate the schema.sql and initial migration.

DB driver is pgx but it can be changed in the sqlc configuration.

Tasks

List all available tasks

task -l

Note: a Makefile is also provided for comparison with Taskfile.

Testing

Run tests with

task test

# or specific folders
task test -- pkg/db

For integration testing consider using docker-compose or task db-up to get a test db instance.

CI

A github actions workflow is configured to run: go fmt, vet, test. The workflow also runs gosec.