Building a Laravel like simple service container in Go in 10 minutes

Aniruddha Chakraborty
2 min readJun 13, 2020

--

I’ve worked on a project that needs to be written in Golang for various reasons. but our discussion is how we would write a simple yet effective and efficient service container in GO.

We know the service container term from the book “Gang of Four”. But most of the developers learned about this code structure from Laravel Or Ruby on rails. The word service container explains itself “it contains the services ”. So depending on the use, we can access the entire application using a very simple access structure. This helps to gain full control of every single corner of your codebase from every single corner of your code.

Seems easy but pretty confusing right? Let’s try to explain with some Go code. Though you can build a service container using reflect module, as you know reflect is very expensive in Go. For us, We would use our knowledge of struct and type assertation.

Our Code Folder structure should look like this.

Simple service container Folder structure.

As you know every folder here represents a package in Golang. So in here container, controllers, providers, and services are different modules.

In the container module let’s write container.go

As you know In Golang interface{} slightly works like Object in javascript. Almost everything means interface{} in Golang but when you need to work with them you might have to use type assertation. Though Go is a functional programming language, here I’m using a singleton design pattern, but you can introduce a factory pattern here if you need it.

Now let’s write a service and put it into the service module.

This is a very simple struct that has a single method in it.

Now I have to register this provider into the provider

We imported the service module and put it into the container.

Now the main magical part.

Here I’m creating a global container first so that I can inject to every provider that needs it, this will be making my container a central controlling ground where I can access anything. Now I’ll initialize the provider struct followed by a function call with container argument. Now When we need those services we must you need to use type assertation, to define which type of service you’re accessing and THAADAAA!

you wrote a mini Laravel in Golang in just 10 minutes. The power of a service container is you can load any connection anywhere anytime. The code will be slightly complex but the control over your code is enormous. This means as far you have the global container variable in your codebase you can access anything anywhere from your codebase anytime.

Note: I tried to explain this in a very simplistic manner so that everyone understands the underlying concept of it.

--

--

Aniruddha Chakraborty
Aniruddha Chakraborty

Written by Aniruddha Chakraborty

Passionate Distributed Systems Engineer. Proficient writing code in Go,Node.js, Typescript, Python, and PHP.

No responses yet