Building Microservices in Go — Part 1 : Project Setup, Dockerization

Saad Farhan
4 min readNov 3, 2022

--

I am a Product Engineer working with multiple startups and tech enthusiasts to build out their technology for them. My team and I had made Python (Flask) our go to stack for backend because of how easily you can use it for rapid app development but the services gets compromised when you have a lot of data to deal with. Python performs better with small applications but for large scale data you always end up incorporating some middleware to make the data transfer faster.

We decided to switch to a different language which is optimized and generates results faster. This is always an extremely difficult decision especially when most of your team is unfamiliar with the new technology. I wanted to try out Go because I’d heard about its performance benchmarks and how its very similar to Java/C++ in terms of speed, optimizations etc. One more reason I was leaning towards was its great eco system and how it has great supports for tools like GCP, RabbitMQ, Reddis etc.

When I started building out the micro-services in Go I realized that it doesn’t have a strong community as Python and I had to spend a lot of time going through trial and errors to finalize the framework, ORM layer, third party configurations. So I decided to write a series of articles on how to build a microservice in Go so that people don’t have to spend a lot of time on these things and can just get started with building out their systems.

Stack

  1. GO Fiber
  2. Beego ORM
  3. Postgres
  4. GCP

I chose Go Fiber because of its minimalist nature and due to its similarity with Flask. On top of it I used Beego’s ORM layer because of its ease of configuration and how simply you can create migrations.

Setting Up the Environment

In this series we are going to be building a mock web service. In our example we will be building out two micro-services users (responsible for authenticating, maintaining user data) and blogs (manage blogs, categories authors). In this part we will setup our basic structure and write a basic endpoint that returns “Hello world” as a string.

Lets start by creating our docker-compose file and define our micro-service in it

This is a pretty self-explanatory compose file in a nutshell we defined our services, the ports it’ll be listening to, its Dockerfile and context.

Next we will create top level services folder. Inside it we will create another folder in correspondence to our service web_blog and in it we will create our docker file for the service. So your directory structure should look like this :

Now we will start writing our docker file

We start by pulling Go 1.18 image and then updating the binaries. After that we specify our work directory and copy the contents of our current directory to the working directory. Next we install the dependencies, build our binary and finally execute the binary.

Now we need to initialize our module. We’ll do that by changing our directory into our service folder by

cd services/web_blog

And then running the following command

go mod init github.com/saadfarhan124/microservices_go/web_blog.git;

You can name your module whatever you’d like but its always a good thing to follow a few conventions. I named this service by appending the service name to my git repository URL.

Running this command will create a go.mod file for us which will handle all our dependencies for us. So now we are going to add our first dependency and that will be the Go Fiber framework. Run the following command in the same directory

go get github.com/gofiber/fiber/v2

Adding in our first dependency will create a go.sum file for us

Next we are going to create a main.go file in the service directory. Your structure should look like this :

Now we are going to start typing some code into our main.go file. First we are going to declare our package name and import our dependencies

Next step is to write our main function

We start by creating a fiber object and then set up a simple get endpoint that returns a string “Hello World”. Finally we start the server by using the Listen function and passing it a port number. Now let’s test out our endpoint by running the following in terminal :

docker-compose up --build

So we managed to get a dockerized micro-service up and running. In part 2 we are going to configure live reloading into our service, add models, migrations and so much more.

You can find the source code here

--

--