A Fictional Bank

Abhishek Amralkar
4 min readJan 17, 2024

--

Photo by Etienne Martin on Unsplash

I am happy to announce A Fictional Bank(AFB). We will be the first virtual bank in the galaxy and will soon roll out our application. The application will support basic bank operations like

  • Deposit
  • Withdrawal
  • Balance

We will write an application in Golang. Why Golang? It’s modern and fast, yes I know it’s not faster than Rust but still it’s fast.

AFB decided to make the application's code open source; below is the GitHub repository.

Below is the code snippet for the version 1.

To decode what the developer did let’s go line by line.

Declare a Variable

In Go we can use var keyword to define a variable

var balance float64

Now we have variable balance defined by the type float64 and we can initialize it

balance = 42

The variable can be defined and initialized in one line too

var balance float64 = 42

Without any assignment, the value of the variable will be Zero Value.

We can define a variable using an operator short assignment operatortoo and it's the easiest way to declare a variable in Go. Short Variable Declaration cannot be used at Package Scope variables.

package main

import "fmt"

func main() {
balance:= 42
}

Functions

We have 3 functional defined

  • balance
  • deposit
  • withdraw
func deposit(m float64) float64 {
if balance < m {
fmt.Println("Insufficient Funds")
}
balance += m
return balance
}

func withdraw(m float64) float64 {
balance -= m
return balance
}

func balance() float64 {
return balance
}

func is the keyword use to declare a function, which takes mtype float64 and add that to balancewhich is a type of float64 with the return state, we can return from the function but you need to define what type you want to return like we are returning a float64 type.

Execute

To run a program we can run

go run main.go

But Go is a compiled language and you can build a binary and ship it to anyone who wants to run it

go build

But the above command will create a binary with the directory name and if you want to create a binary with some other name

go build -o afbapp

Binary will be created in your project root directory.

ls -la
total 1788
drwxrwxr-x 3 aaa aaa 4096 Jan 14 23:18 .
drwxrwxr-x 12 aaa aaa 4096 Jan 12 23:17 ..
-rwxrwxr-x 1 aaa aaa 1803214 Jan 14 23:18 afbapp
drwxrwxr-x 8 aaa aaa 4096 Jan 14 23:18 .git
-rw-rw-r-- 1 aaa aaa 63 Jan 14 23:18 go.mod
-rw-rw-r-- 1 aaa aaa 1312 Jan 12 23:17 LICENSE
-rw-rw-r-- 1 aaa aaa 314 Jan 14 22:25 main.go

Ohh BTW we should always initialize the project with go mod init which creates a go.mod file in your root directory.

Also by default the binary will be created for your current OS and CPU Architecture type.

Below is the list of known OS and Arch supported by Golang

# get the above list 

go tool dist list

To get the current GOOS and GOARCH

go get GOOS GOARCH

# Output
linux
amd64

To build for the macOS

GOOS=darwin go build -o afbapp

To build for the macOS and arm

GOOS=darwin GOARCH=arm64 go build -o afbapp

Let’s Dockerize

Running binary is Okay, but Docker is the de facto standard to build and ship applications; hence, we decided to Dockerize our bank application too.

We are using `golang:1.21-alpine3.19` as our base image copying all the files in the repo to the base container and building the binary. Once binary is created we copy it to the runner container, so we are using a 2-stage docker build.

Makefile

Well, we want to have a strong toolset built around our application development and we want to reduce the time spent in building binary or container. Using Makefile we can automate

Some of the commands which Makefile supported in our case right now are

make build
make run
make dbuild
make drun

Improvements

The app is working but it's not at all a production-level app and hence it needs improvement. We will improve the app in the next release.

--

--