Stack Name - Golang

Synopsis

The is the base reusable implementation for a Go type tech stack.  Go is an open source programming language supported by Google.

The core features of Go include:

  • it is a statically typed
  • it is compiled programming language
  • it is syntactically similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency

The Harbormaster Go tech stack contains the necessary templates and macros to generate a fully functional Go project complete with a vendor specific YAML pipeline file in order to install Go and the necessary dependencies to build, test, and deploy the resulting application.

Importantly, this tech stack uses gorm, an Object to Relational mapping library with support for MySQL, Postgres, SQLite, and SQL Server.

Details

Authordev@harbormaster.ai
AccessPublic
Derived From.common.tech.stack
Long NameGo Tech Stack
Short NameGolang
Language(s)Golang, YAML, XML, Velocity Macros
Git Urlhttps://github.com/Harbormaster-AI/techstacks/tree/main/.go.tech.stack

Options

Option NamedescriptionTypevalues
go
appPortPort for Go app to listen oninputdefault:8088
dbTypeSupported database typesselect

default:mysql

  • mysql
  • postgres
  • sqlite
  • sqlserver
dbUserNameName required to login to the databaseinput 
dbPasswordPassword required to login to the databaseinput 
dbNameName of the application databaseinput 
dbHostDatabase hostinput 
dbArgsDatabase args to append to connection statementinput 
dbDisableForeignKeyConstraintWhenMigratingKeep to true if the model contains associationsbooleandefault:true

Contents

  • Build/CICD:
    • Vendor specific handlers using Velocity template files and macros to generate a pipeline YAML file for CI functionality.
      Note: CircleCI is the only supported CI/CD platform.  The rest are soon to follow
  • Src/Controller:
    • Controllers used to handle URL invocation, business logic required for a given invocation and redirect to the DAO layer for read/write from/to the ORM layer
  • Src/DAO:
    • Data access objects that interact with gorm to handle all CRUD activities with each model entity, including the contained data for an entity and its associations
  • Src/Json:
    • Handlers for formatting Request data to and from a JSON structure, and forces the HTTP header Content-Type  to “application/json”
  • Src/Model:
    • Go struct declarations for each entity and enumeration found in the model.  Includes an entities intrinsic data and associations with other structs
  • Src/Router:
    • Route handler for application HTTP requests for all CRUD capabilities including association related actions
  • Src/Test
    • Leverages the Go test facility, contains the test handlers of core CRUD functionality for each model entity
  • Src/Utils:
    • General handler for
      • establishing a single database connection
      • unmarshiling the HTTP Request body into a JSON object

Usage

If using a CI/CD platform such as CircleCI, AWS Codebuild, Jenkins, etc.., it is best to refer to the Pipeline YAML file generated for that platform.

If not using a CI/CD platform refer to the instructions below on how to use the generated project manually.

To get started manually

The following instructions assume Go is installed. If not, learn how to install GO here.

Install Dependencies

Go requires Git to be installed in order pull the required dependencies.

RUN go get github.com/gorilla/mux
RUN go get -u gorm.io/gorm
RUN go get github.com/joho/godotenv
RUN go get -u gorm.io/driver/mysql

Change the reference to mysql to whatever of the dependent database types is in use (postgres, sqlite, or sqlserver)

Change To Project Directory

To continue, change the current directory  src/ where is the name given to the application during project generation.

cd /src/

List the directories content to verify being in the correct directory by seeing main.go in the list

Initialize

RUN go mod init <>
RUN go mod tidy

where <> is the name given to the application during project generation.

Build

go build

Run Test

go test /api/test