Skip to main content

Command Palette

Search for a command to run...

Go Lang Guide

Published
5 min read
Go Lang Guide

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/")

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

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/")

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

[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")

  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

5. References