Introduction
The fact that you have landed on this page is a strong indication that you are aware of the existence of the go language.
The language itself was originally created by three Google developers back in 2007 (though officially released to the world in 2009) in their spare time, but it has since gone on to be one of the hottest new languages on the market and is even used by Google to power some of their own services.
Although a relatively young language, there is a big community of developers using it, and some of these are approaching the language in very much a craftsman like manner, and these developers are looking for ways to use principles like SOLID with it.
This is not a post about the merits of SOLID in go, what I want to cover here is one aspect of SOLID, Dependency Inversion Principle, and how you can apply DIP to go code.
The language itself was originally created by three Google developers back in 2007 (though officially released to the world in 2009) in their spare time, but it has since gone on to be one of the hottest new languages on the market and is even used by Google to power some of their own services.
Although a relatively young language, there is a big community of developers using it, and some of these are approaching the language in very much a craftsman like manner, and these developers are looking for ways to use principles like SOLID with it.
This is not a post about the merits of SOLID in go, what I want to cover here is one aspect of SOLID, Dependency Inversion Principle, and how you can apply DIP to go code.
Requirements of Dependency Inversion
The principle of Dependency Inversion, is to remove hardwired dependencies from any code that you write, further more functions, structs, and even packages should only depend on abstractions of other modules, and not the module itself.
This can be achieved in many ways, and some of the standard methods are:
This can be achieved in many ways, and some of the standard methods are:
- Separated Interface Pattern
- Dependency Injection
- Inversion of Control
- Adapter Pattern
DI Options
So what are my options as far as Dependency Injection is concerned in the go language. Well this is not an exhaustive review of all of the libraries, and if you know of others that you think are worth exploring please feel free to let me know, but at the time of writing this post the three packages below stood out as promising.
Over the next couple of posts I will go through each option detailed above, give some background about each, as well as, with the use of a simple example discuss the good and bad points of each option.
Next: Simple IoC >>
- go-inject
- go.inject
- depinject
Over the next couple of posts I will go through each option detailed above, give some background about each, as well as, with the use of a simple example discuss the good and bad points of each option.
Next: Simple IoC >>
Links
Dependency Inversion in Go - IntroductionDependency Inversion in Go - Simple IoC
Dependency Inversion in Go - go-inject
Dependency Inversion in Go - go.inject
Dependency Inversion in Go - depinject
Dependency Inversion in Go - Summary