Giter Site home page Giter Site logo

golang-starter-kit's Introduction

Golang Starter Kit

A starter kit written in Golang + Ports and Adapters structure on top of https://github.com/golang-standards/project-layout

Getting Started

  1. git clone [email protected]/yuraxdrumz/golang-starter-kit
  2. go install github.com/golangci/golangci-lint/cmd/[email protected]
  3. Run brew install pre-commit / pip install pre-commit
  4. Run pre-commit install

If you want to check pre-commit hook on current folder run pre-commit run --all-files

Prerequisites

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:

  1. Create appropriate structs in ports.go file under internal/app/your-use-case/ports.go
  2. Create your use-case with your application specific logic under internal/app/your-use-case/logic.go
  3. Create your in/out adapter, for example - Repository(out) or gRPC(in). internal/pkg/adapters/*
  4. Tests!!! your-file-name_test.go under same directory as file internal/app/your-use-case/logic_test.go

Installing Packages

go get -u <repository-name>

Running the service

go run main.go

Environment Variables

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.

Logging

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

Pre Commit

You can add any pre commit hooks to .pre-commit-config.yaml if needed

golang-starter-kit's People

Contributors

yuraxdrumz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.