Skip to content

Go API Template

ID: go-api

A high-performance REST API built with Go and the Gin framework. Includes PostgreSQL integration via database/sql, a Makefile for common Go tasks, and Docker support.

Prerequisites

Go 1.21 or higher must be installed. Check with:

bash
go version

What's Included

my-go-api/
  go.mod                  # Go module definition
  go.sum                  # Dependency checksums
  Makefile                # Common tasks: build, test, lint, run
  README.md               # Project-specific setup guide
  .gitignore
  .env.example            # Documented environment variables
  Dockerfile              # Production container image
  docker-compose.yml      # Local development stack with PostgreSQL
  cmd/
    server/
      main.go             # Application entry point
  internal/
    handler/
      health.go           # GET /health handler
    router/
      router.go           # Gin router setup
    db/
      db.go               # Database connection

Stack

LayerTechnologyVersion
LanguageGo1.21+
HTTP frameworkGinLatest
DatabasePostgreSQL14+
ContainerDockerAny

Usage

Scaffold a new Go API project:

bash
npx forgekit-cli new my-go-api --template go-api

Or use the interactive wizard:

bash
npx forgekit-cli new

Running After Scaffold

With Docker (recommended):

bash
cd my-go-api
docker-compose up --build

API runs at http://localhost:8080.

Without Docker:

bash
cd my-go-api
cp .env.example .env   # configure DATABASE_URL
make run

Makefile Targets

TargetDescription
make runRun the server locally
make buildCompile the binary to ./bin/server
make testRun the test suite
make lintRun golangci-lint
make tidyRun go mod tidy

Health Check

GET /health

Response:

json
{
  "status": "ok",
  "timestamp": "2026-03-22T10:00:00.000Z"
}

Environment Variables

VariableDescriptionDefault
PORTPort the server listens on8080
DATABASE_URLPostgreSQL connection stringSee .env.example
GIN_MODEGin mode: debug or releasedebug

Customization Tips

Add a new endpoint: Create a handler function in internal/handler/, define the route in internal/router/router.go, and write a corresponding test.

Add database migrations: Use golang-migrate or goose. Add migration files to a migrations/ directory and wire the migration step into main.go.

Deploy to production: Set GIN_MODE=release and DATABASE_URL in your deployment environment. The Dockerfile uses a multi-stage build to produce a minimal final image.

Released under the Apache 2.0 License.