Going with Go: Exploring the Language

Abiral Sthapit
4 min readFeb 24, 2024

--

Hi, It’s been six and a half months since I started with Go programming mostly fixing bugs and writing small features on already existing projects, with serverless framework so I have no way of measuring if I can go with GO.

I am doing this to see where I am with the Go, I hope with this I can find some of the pain points and help someone who is thinking of starting with GO.

Project: myMoneytracker
Features: Go server, authentication(jwt), db(TBD), Clean Architecture

phase_1: A simple wallet application where users can track their expenses

# terminal Create app folder and setup project structure
mkdir myExpenseTracker
cd myExpenseTracker

# Intialize
go mod init github.com/abrialstha/myMoneytracker
git init
yarn init -y

# Project Strucutre
mkdir bin cmd internal
touch .env .gitignore Makefile cmd/main/main.go

# Open in Editor of your choice
code .

Let's Create a quick build file first to compile and run our project.

Now our necessary Hello World!

package main

import "fmt"

func main() {
fmt.Println("Hello World!")
}

with this in place, we can finally test our makefile. Now let’s bring in the server, I am using fiber for this application but any package like gin or gorilla/mux is also great. For more hardcore Chad out there net/http is a very solid option, comes with all the support and features that we may ever need.

go get github.com/gofiber/fiber/v2

Up to this point was very easy and I had experience with express before.

Let’s finally get started with the project, I want to simply create a vault first before doing any complicated task just to test out the water before diving too deep.

I am going to further organize my code in the following way inside the internal folder. The package(folder) routes will contain all the routes for the application and utils will contain helper functions for anything that I may need. the domain will contain the core application logic, the store is my repository and the infrastructure will contain the logic on external clients like databases.

internal/
├── domain
├── infrastructure
├── routes
├── store
└── utils

lets create our store(repository) first

Here I am simply creating a vault that has some balance and metadata such as bank name and owner. I am missing the updated date and created date which I will be adding in the future, for now, let's make it simple and clean.

Now Creating a simple Infrastructure for the vault storage, I am using an in-memory database for now, but the idea behind this is to simply be able to replace this with any other dbClient say Postgres or MongoDB, etc

Now we can create a domain, here it looks like we are repeating ourselves and also writing a lot of unnecessary code, but the power of this approach is we can simultaneously work on the main application without worrying about the actual implementation part, say you are working in a team and your task is to work on the domain part and your team is responsible for the infrastructure part. As long as you both are referring to the store repository interface, there will not be any complication and you can easily mock the store action like the above, here you can also use the power of generative code AI like a copilot, codium, code whisper, etc they are really handy for generating the mock functions and code completions. Also In the future, if we ever want to change anything we can easily introduce changes and not worry a lot about breaking the application unknowingly.

Finally, to wrap up this post, I am going to create a route and hook it up with our main application.

Next, we will introduce some sort of activity logs for the application, also let's create some middleware for logs and we have not used our env yet so also we will see how we can easily set env in .env file and get the env for our file. 👋 Until then Stay caffeinated. ☕

--

--

No responses yet