container

package module
v1.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2022 License: MIT Imports: 1 Imported by: 0

README

Container

Central container for managing dependencies and performing dependency injection.

Mote: Container use Caster for get dependencies by type.

Create New Container

import "github.com/bopher/container";
cnt := container.NewContainer();

Usage

Container interface contains following methods:

Register

Add new dependency to container

// Signature:
Register(name string, dep any)

// Example:
cnt.Register("app-name", "My App")
Resolve

Get dependency. this function return false as second return value if dependency not exists.

Caution: Resolve return data as any type. if you need to parse data as special type use helper parser methods described later.

// Signature:
Resolve(name string) (any, bool)

// Example:
appName, exists := cnt.Resolve("app-name") // => "My App", true
Exists

Check if dependency exists.

// Signature:
Exists(name string) bool

// Example:
exists := cnt.Exists("app-name") // => true
Cast

Parse dependency as caster.

// Signature:
Cast(name string) caster.Caster

// Example:
v, err := cnt.Cast("timeout").Int()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container interface {
	// Register add new dependency to container
	Register(name string, dep any)
	// Resolve get a dependency
	Resolve(name string) (any, bool)
	// Exists check if dependency exists
	Exists(name string) bool
	// Cast parse dependency as caster
	Cast(name string) caster.Caster
}

Container dependency manager interface

func NewContainer

func NewContainer() Container

NewContainer create a new container instance

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL