Go Lang Guide

Andre Vianna
My Dev Zone
Published in
6 min readJan 1, 2022
https://go.dev/

Summary

  1. Concepts of Go Lang
  2. Concepts of MicroService
  3. Application Payment Gateway
  4. Let’s Code in Go
  5. References

1. Concepts of Go Lang

Build fast, reliable, and efficient software at scale

  • Go is an open-source programming language supported by Google
  • Easy to learn and get started with
  • Built-in concurrency and a robust standard library
  • A growing ecosystem of partners, communities, and tools

How to Download

Docker Official Go Lang

docker pull golang

Start a Go instance in your app

The most straightforward way to use this image is to use a Go container as both the build and runtime environment. In your Dockerfile, writing something along the lines of the following will compile and run your project:

FROM golang:1.17WORKDIR /go/src/app
COPY . .
RUN go get -d -v ./...
RUN go install -v ./...
CMD ["app"]

You can then build and run the Docker image:

$ docker build -t my-golang-app .
$ docker run -it --rm --name my-running-app my-golang-app

2. Concepts of MicroServices

Monolith vs Microservices Architecture

Monolithic Architecture
MicroService Architecture
Using REST Interfaces to expose microservices
Monolithic use a Central Data Base
Microservices have its own private database and they can’t directly access the database owned by other microservices
Client-side discovery
Server-side discovery
Building and deploying microservices as containers
Microservice security with OAuth 2.0 and OpenID Connect
Active composition of microservices
All microservices are exposed via an API-gateway
Hybrid composition
Service Mesh in Action
Realizing microservices architecture with WSO2 products and technologies.

What are microservices?

  • Microservices are small, independent, and loosely coupled. A single small team of developers can write and maintain a service.
  • Each service is a separate codebase, which can be managed by a small development team.
  • Services can be deployed independently. A team can update an existing service without rebuilding and redeploying the entire application.
  • Services are responsible for persisting their own data or external state. This differs from the traditional model, where a separate data layer handles data persistence.
  • Services communicate with each other by using well-defined APIs. Internal implementation details of each service are hidden from other services.
  • Supports polyglot programming. For example, services don’t need to share the same technology stack, libraries, or frameworks.

Microservices Styles?

4. Application Payment Gateway

Components of Application

Components Payment Gateway

  • NextJS = Frontend based Java Script
  • Nest.JS = Backend, Update DataBase
  • Apache Kafka = Massage Process
  • Go Lang = MicroService Processing Payment
  • Prometheus = Metrics
  • Grafana = Dashboard
  • Docker = Container
  • Kubernetes = Orchestration, Deploy, Production

Domain Driven Design (DDD)

Transaction Processing in GoLang

Application Domain Rules

  • A Transaction will have only two states: Approved or Reject
  • The Minimum Amount for each transaction is $1.00
  • The Maximum Amount for a Transaction to be approved is $1000
  • Any Transaction between the amounts of $1 and $1,000 will always be approved
  • For a transaction to be approved, the credit card details must be valid.

Use Case: “Process Transaction”

  • You will receive the data of a transaction
  • Will Create a Transaction
  • Will add credit card to this transaction:

1) If the Credit Card is Invalid:

  • The transaction data will be inserted into the database with status=rejected containing the error message
  • The Transaction will be published on Apache Kafka

2) If the Transaction is NOT Approved:

  • The transaction data will be inserted into the database with status=rejected containing the error message
  • The Transaction will be published on Apache Kafka

3) If the Transaction is Approved:

  • The transaction data will be inserted into the database with status=approved
  • The Transaction will be published on Apache Kafka

DDD Adapters

Payment Gateway Adapters

1. Apache Kafka

  • Message Consumption
  • Message Production

2. Database

  • Database Connection
  • Inserting the Transaction into the Database
  • SQLite / MySQL
  • Repository Pattern

3. Presenter

  • Will set the default of the message to be sent via Apache Kafka
  • Apache Kafka Message Format

Clean Architecture Fundamentals

How would you think your software if…

  • Major Changes in Business Rules
  • Database Change
  • Support Multiples Protocolos: REST, gRPC, Kafka, RabbitMQ, GraphQL
  • CLI

Clean Architecture Elements

  • Entities → Business Rules
  • Use Cases
  • Controllers
  • Presenters
  • Gateways
  • DB
  • UI
  • External Interfaces

4. Let’s Code in Go

Github Repository

  1. Go Lang Installtion

curl -OL https://golang.org/dl/go1.17.5.linux-amd64.tar.gz

sha256sum go1.17.5.linux-amd64.tar.gz

bd78114b0d441b029c8fe0341f4910370925a4d270a6a590668840675b0c653e go1.17.5.linux-amd64.tar.gz

sudo tar -C /usr/local -xvf go1.17.5.linux-amd64.tar.gz

  • sudo nano ~/.profile
  • source ~/.profile
  • go version
  • go version go1.17.5 linux/amd64

Testing Go

  • mkdir hello-go
  • go mod init your_domain/hello
  • hello.go

package main

import “fmt”

func main() {
fmt.Println(“Hello, World!”)
}

go run .

Go Lang with Docker Container

  • docker exec -it aluno_app_1 bash

--

--

Andre Vianna
My Dev Zone

Software Engineer & Data Scientist #ESG #Vision2030 #Blockchain #DataScience #iot #bigdata #analytics #machinelearning #deeplearning #dataviz