Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/ro-29/dependency-injection-go

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/ro-29/dependency-injection-go

  • v0.0.0-20190817100812-be5267bef70b
  • Source
  • Go
  • Socket score

Version published
Created
Source

Dependency Injection in GO

GO Dependency injection example

➜ go run main.go

** Init DI Provider A **<
** Init serviceA **
** Init DI Provider B **
** Init serviceB **
from A:
hey there:  testing
** Init DI Provider C **
** Init serviceC **
** Using already init DI Provider B **
from C:
hey there:  testing

Motivation

Go uses struct and interfaces to declare a service. A service can inherit another service and as many it wants ( but we must try to keep it minimal for loosely coupled dependency graph)

This tutorial assumes all the go services must be initialized at the program initiation not at the run time of program.

Now the problem can arise if a service is required by multiple services! It will essentially means we will be initializing a service as many times as it is required by other services. It's not an ideal scenario in a case service that is being initialized multiple times has a network related dependency like database initialization or some other complex initialization.

Lets view this problem from the famous diamond perspective but there is no actual inheritance here just the service dependency angle. (Though go supports inheritance but this is out of scope of this tutorial)

      A
    B   C
      D

**Incomplete

FAQs

Package last updated on 17 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc