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

gopkg.in/gookit/di.v1

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gopkg.in/gookit/di.v1

  • v1.0.1
  • Go
  • Socket score

Version published
Created
Source

DI

GoDoc Build Status Coverage Status Go Report Card

中文说明

Lightweight dependency injection container implements by Go.

GoDoc

Install

go get github.com/gookit/di

Usage

package main

import (
    "github.com/gookit/di"
)

func main() {
    box := di.New("my-services")
    
    // add a simple value
    box.Set("service1", "val1")
    
    // register a callback func.
    box.Set("service2", func() (interface, error) {
    	return &MyApp{}, nil
    })
    
    // register a factory func.
    box.Set("service3", func() (interface, error) {
    	return &MyObject{}, nil
    }, true)
    
    // get 
    v1 := box.Get("service1") // "val1"
    
    // is a singleton value. Notice: v2 == v3
    v2 := box.Get("service2").(*MyApp)
    v3 := box.Get("service2").(*MyApp)
    
    // is factory func. Notice: v4 != v5
    v4 := box.Get("service3").(*MyObject)
    v5 := box.Get("service3").(*MyObject)
}

API Methods

  • func (c *Container) Set(name string, val interface{}, isFactory ...bool) *Container
  • func (c *Container) Get(name string) interface{}
  • func (c *Container) SafeGet(name string) (val interface{}, err error)
  • func (c *Container) Inject(ptr interface{}) (err error)

Refer

License

MIT

FAQs

Package last updated on 06 Dec 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