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

github.com/lsegal/dinit

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/lsegal/dinit

  • v0.0.0-20180318001259-fb693a6d64cb
  • Source
  • Go
  • Socket score

Version published
Created
Source

DInit

GoDoc Build Status Coverage Status

DInit is a library to initialize structs using Dependenci Injection (DI).

Installation

go get -u github.com/lsegal/dinit

Usage

Use dinit.Init(val1, val2, ...) where arguments are either struct values, references, bare functions, or initializer functions that return struct values.

DInit will then call your functions in the appropriate order to ensure that all functions have their arguments filled by the values provided in the Init() call.

For example, in the code below we have a log.Logger object, a client and service. The service depends on log.Logger, and client depends on both service and log.Logger. When we call their initializers, only 1 instance of client and server are created:

func newClient(l *log.Logger, svc lister) *client { /* ... */ }
func newService(l *log.Logger) *service { /* ... */ }

func main() {
  l := log.New(os.Stdout, "", log.Lshortfile)
  useClient := func(c *client) { c.PrintPeople() }
  dinit.Init(newClient, useClient, newService, l)

  // Output:
  // main.go:40: Initializing service
  // main.go:35: Initializing client
  // main.go:30: Client asked for a list of people
  // main.go:18: People: Sarah, Bob, André
}

You can see the full (runnable) example in examples/client_server/main.go.

Copyright (c) 2018 Loren Segal. All rights reserved.

This project is licensed under the MIT license, see LICENSE for details.

FAQs

Package last updated on 18 Mar 2018

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