Let’s GO!!! Dependency Injection in GOlang

golang

Starting in GOlang (few points about Java)

After almost ten years of programming in Java, I decided to start to learn a different language.

But Why??? Java is not good enough??

Big NO here, for me Java is brilliant and awesome language and the community is so vibrant as well. I love coding in java. I love how people who’re working in java practice Oriented Object Programming.

But in my opinion, nowadays Java addressing Enterprise World. I mean it is an excellent language to code for Business requirements, like CRM and other things related a business. There are several exciting frameworks about persistence, web which increase developer productivity and help delivery code in production.

Nowadays, my challenge is about creating cloud-native systems for infrastructure stuff. I mean the main purpose of system is helping developers to create amazing microservices and delivery it in a secure and managed way. That is the reason why I choose the golang language.

Dependency Injection Pattern

I will not deep dive into dependency injection pattern, because there are a lot of incredible blog post, articles and discussions about that.

Look at the Martin Fowler blog to find an amazing article about that.

For now, Dependency injection is important to create decoupled and well-designed code.

We will use WIRE to help us to implement Dependency Injection pattern in Golang.

Requirements

I’ve created the go project using Go Modules, it is an interesting way to manage the dependencies.

Installing Wire

Wire generates the necessary stuff at compilation time, then we need to install wire to be able to do it. easy peasy lemon squeezy..

and ensuring that $GOPATH/bin is added to your $PATH.

You can use the full instructions here.

Let’s code a little bit

Go Dependencies

We will create a simple random words generator using Babler, A small utility to generate random words in #golang.

I’m using the IntellijIDEA. The IDE has the autocomplete feature to add dependency automatically in the go.mod, but if you are using the VSCODE IDE which is good as well you can add the go.mod described below

There are a couple of dependencies, the two most important are Wire and Babler.

Business Code

Now, we will create a simple file with our “business code”, it will very simple. Let’s look at the code

There are a few important things here, let’s discuss

Producers

Let’s look at the New* functions. It is our producers, the main goal of these functions is to produce things to be injected. It is very important because we will need to instruct Wire how to create or produce it, we will understand as soon. Very simple code here we will create a pointer for each struct.

Injection Points or Clients

Another important part of the code is WordGenerator struct declaration. As we can see we need a Dictionary pointer, let’s understand how we are able to receive it.

On the NewWordGenerator function, as we can see we received the pointer to Dictionary, that is our injection point. The wire will “inject” the dictionary reference here.

We don’ care about how the Dictionary was created and injected, the only thing we need to think is “I want the instance here, this instance should be ready to me” in simple words. Wire framework will take care of injection for us, that is the important principle about dependency injection.

Wire Configuration

Now we know about the main characteristics of our application let’s instructs wire how to create our objects.

In the root folder create a file called wire.go, this name is mandatory.

Let’s look at the file content.

In the first line, we instruct to go build to ignore this file. This file is necessary to generate our compile file with all dependencies configured it is fantastic.

There are some imports and finally the SetupApplication function, that is the core for our application.

Let’s look at the return. It will produce a WordGenerator, this struct contains our business logic. Our main.go will invoke this struct to generate random words. Sometimes you can create your Application configuration, is up to you.

In our function body, we’ve used wire to create our container. I mean our dependencies to enable other structs to use.

Look at the builder functions as we saw before these functions produce pre-configured structs look at Producers Section

The return of this function doesn’t matter, the important part here is “we explain to wire how to create our application container”.

Wire Code Generation

Now, we are ready to generate our code, as we saw before wire will generate the file to build our application at compilation time, then Let’s do it.

On the root folder, at the same wire.go level type:

The tool will create a file called wire_gen.go, let’s analyze the content

In the first line we have a warning, “Do not edit” that is very import thing to notice.

Then we have the application configuration, look at the declaration all of our dependencies are built by the tool, amazing thing here. All of the structs are configured and ready to use.

The wire did the “dirty job” for us. I’m so proud..hahaha

Main.go

Now our dependencies are ready to use, let’s use. Create a file called main.go

Look at the SetupApplication() invocation, it will produce our WordGenerator then we can call the GenerateWord function, easy easy.

Conclusion

I like what java programmers are creating using some important patterns like Dependency Injection, and for me, wire is a vital library if you are thinking to work professionally with go.

It will increase your productivity and also will help you to create decoupled applications.

The GitHub repo is available here.

More complicated stuff

If you want something more real I’ve coded a simple application which will receive the HTTP Request and persist in the PostgreSQL database using Wire.

The code can be found here.

References

Wire tutorial

Wire Userguide

Author: de Oliveira Claudio Eduardo

Claudio Eduardo de Oliveira is a software architect and software developer working for Sensedia. He works with APIs, microservices, and cloud-centric applications. He has more than ten years of experience in software development with Java and JVM languages. He is a speaker at some important events in Brazil about Spring and other Java Frameworks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.