# Go Lang Guide

[https://go.dev/](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**

[**Downloads**  
*After downloading a binary release suitable for your system, please follow the installation instructions. If you are…*go.dev](https://go.dev/dl/ "https://go.dev/dl/")[](https://go.dev/dl/)

**Docker Official Go Lang**

[**Golang - Official Image | Docker Hub**  
*Go (golang) is a general purpose, higher-level, imperative programming language.*hub.docker.com](https://hub.docker.com/_/golang "https://hub.docker.com/_/golang")[](https://hub.docker.com/_/golang)

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.17
```

```
WORKDIR /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

[**Microservices in Practice - Key Architectural Concepts of an MSA**  
*Enterprise software applications are designed to facilitate numerous business requirements. Hence, a given software…*wso2.com](https://wso2.com/whitepapers/microservices-in-practice-key-architectural-concepts-of-an-msa/ "https://wso2.com/whitepapers/microservices-in-practice-key-architectural-concepts-of-an-msa/")[](https://wso2.com/whitepapers/microservices-in-practice-key-architectural-concepts-of-an-msa/)

**Monolith vs Microservices Architecture**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833293679/-IXYlR2tS.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833294981/felq-YnXP.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833296240/IHlwNucUe.png)

Monolithic Architecture

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833297665/oy095qsXN.png)

MicroService Architecture

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833298845/Sf-JdNPqF.png)

Using REST Interfaces to expose microservices

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833300067/Td-LVPnPp.png)

Monolithic use a Central Data Base

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833301294/oVbD-yukm.png)

Microservices have its own private database and they can’t directly access the database owned by other microservices

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833302628/ctnz_dZX-.png)

Client-side discovery

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833303905/Lk7-qVkOU.png)

Server-side discovery

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833305244/-EIpGPayGN.png)

Building and deploying microservices as containers

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833306520/2tCNvs5p8.png)

Microservice security with OAuth 2.0 and OpenID Connect

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833307837/ldcfL186S.png)

Active composition of microservices

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833309180/rEb46XVBs.png)

All microservices are exposed via an API-gateway

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833310410/sWSryJcTp3.png)

Hybrid composition

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833311703/h0bGzCprl.png)

Service Mesh in Action

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833313100/PbGuxFWee.png)

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?

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833314394/Pf3ts_4sM.png)

### 4\. Application Payment Gateway

**Components of Application**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833315815/TzlN6zsNT.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833317711/kZLYYRgp_a.png)

### 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)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833319066/Xqk_o3QJG.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833320281/VHgtG4EGS.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833321635/NhaMpdp4A.png)

### Transaction Processing in GoLang

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833323087/qOwwyJ7hU.jpeg)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833324366/Gaazy53cC.png)

### 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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833325707/NYFsbCpIW.png)

**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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833327179/zI2qcpfej.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833328675/EG_Flqapj.jpeg)

### 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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833330076/EiW6ZDGg1.jpeg)

### How would you think your software if…

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833331483/fOb6cQoxV.png)

### Clean Architecture Elements

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

### 4\. Let’s Code in Go

**Github Repository**

[**GitHub - Software-Engineering-2030/Go-Lang-Guide: Go Lang Guide Repository**  
*You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…*github.com](https://github.com/Software-Engineering-2030/Go-Lang-Guide "https://github.com/Software-Engineering-2030/Go-Lang-Guide")[](https://github.com/Software-Engineering-2030/Go-Lang-Guide)

1.  **Go Lang Installtion**

curl -OL [https://golang.org/dl/go1.17.5.linux-amd64.tar.gz](https://golang.org/dl/go1.16.7.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 .

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662833332908/ZcdlG_KLM.png)

### Go Lang with Docker Container

*   docker exec -it aluno\_app\_1 bash

### 5\. References

*   [https://go.dev/](https://go.dev/)
*   [Microservices for the Enterprise](https://www.amazon.com/Microservices-Enterprise-Designing-Developing-Deploying/dp/1484238575)
*   [Application Integration for Microservices Architectures: A Service Mesh Is Not an ESB](https://www.infoq.com/articles/application-integration-service-mesh/?useSponsorshipSuggestions=true&itm_source=articles_about_architecture-design&itm_medium=link&itm_campaign=architecture-design)
*   [https://martinfowler.com/articles/microservices.htm](https://martinfowler.com/articles/microservices.html)
