Skip to content
/ go-boilerplate Public template

A project boilerplate to make a go application with Fiber

Notifications You must be signed in to change notification settings

kondohiroki/go-boilerplate

Repository files navigation

go-boilerplate 🚀

Go Report Card Go with Confidence

This boilerplate is intended to be used as a starting point for a go application. It is not intended to be used as a but it is can be.

Getting Started

Prerequisites

  • Go 1.20
  • Docker
  • sonar-scanner - for coverage test in local
    brew install sonar-scanner

Installation

  1. Clone the repo
    git clone https://github.com/kondohiroki/go-boilerplate.git
  2. Install Go dependencies
    go mod download
  3. Copy the default configuration file
    cp config/config.example.yaml config/config.yaml
  4. Start the database
    docker compose up -d
  5. Migrate Database
    go run main.go migrate
  6. Run the application
    # Run normally
    go run main.go serve-api
    
    # Run with hot reload
    air serve-api
  7. Testing (optional)
    # Run unit-test
    make unit-test
    
    # Run api-test
    make api-test
    
    # Create sonar scret
    touch .sonar.secret
    echo "your-sonar-token" > .sonar.secret
    
    # Add secret to .sonar.secret
    # Get from sonar web

Standard and Styles Guide

Coding Standard

  1. For those const, use capitalized SNAKE_CASE for public constant. For private, constant name should led by _ (underscore).

    Good Example

    // public
    const BAD_REQUEST int = 400
    
    // private
    const _UNAUTHORIZED int = 401

    Bad Example

    const BadRequest   int = 400
    const unauthorized int = 401

How to Use

Configuration

  • config/config.yaml (ignored by git)
    • Default configuration file
  • cmd/root.go
    • config/config.yaml is loaded by default
    • You can specify the configuration file with the --config flag
  • internal/app/<your-handler>/<xxx>.go
    • Define your handler functions for your endpoint
  • internal/logger/zap_logger.go
    • You can see the log settings in the NewZapLogger function
  • job/
    • You can add your own jobs here
  • scheduler/scheduler.go
    • You can schedule your jobs here
    • You can configure the cron expression in config/config.yaml

Supported Features

  • Configuration with YAML
  • Logging with Zap Logger
  • CLI with Cobra
  • Scheduler with Cron
  • PostgreSQL
  • Redis Cache
  • Docker
  • Fiber Router
  • Add Redis Reliable Queue

Use Cases

  • As a Web Server
    • HTTP API
    • gRPC API
  • As a CLI Application
  • As a Scheduler for Cron Jobs

Roadmap

  • Add gRPC API
  • Document the code