container

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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/gomig/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
⌂ 换网址