
Security News
New CVE Forecasting Tool Predicts 47,000 Disclosures in 2025
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
github.com/marcosxz/dix
dix is golang simple dependency inject (di) kit lib
type XFieldProvider struct{}
func (XFieldProvider) Symbol() string {
return "xfp"
}
func (XFieldProvider) Provide(ctx context.Context, tag *Tag) (any, error) {
return "x_field", nil
}
// X is a target di struct
type X struct {
Field1 string `dix:"from:xfp"`
Field2 string `dix:"from:xfp"`
}
func main () {
// Logging dix log
dix.Logging(true)
// Binding Provider using namespace `def`
dix.Binding[dix.Provider](XFieldProvider{})
// Call DI method make a di object
x, err := dix.DI[X](context.Background())
if err != nil {
// ...
}
// Print x
fmt.Println(x.Field1, x.Field2)
}
// X is a target di struct
type X struct {
Field1 string `dix:"from:xfp;namespace:ns1"`
Field2 string `dix:"from:xfp;namespace:ns2"`
}
func main () {
// Logging dix log
dix.Logging(true)
// Binding Provider using namespace `ns1, ns2`
dix.Binding[dix.Provider](XFieldProvider{}, "ns1", "ns2")
// Call DI method make a di object
x, err := dix.DI[X](context.Background())
if err != nil {
// ...
}
// Print x
fmt.Println(x.Field1, x.Field2)
}
// StringerImpl implement fmt.Stringer
type StringerImpl struct{
}
func (StringerImpl) String() string {
return "string_impl"
}
// X is a target di struct
type X struct {
Field1 fmt.Stringer `dix:"from:?;namespace:ns1"`
Field2 fmt.Stringer `dix:"from:?;namespace:ns2"`
}
func main () {
// Logging dix log
dix.Logging(true)
// Binding fmt.Stringer implement using namespace `ns1, ns2`
dix.Binding[fmt.Stringer](StringerImpl{}, "ns1", "ns2")
// Call DI method make a di object
x, err := dix.DI[X](context.Background())
if err != nil {
// ...
}
// Print x
fmt.Println(x.Field1, x.Field2)
}
// X is a target di struct
type X struct {
Field1 string `dix:"from:?;namespace:ns1"`
Field2 int `dix:"from:?;namespace:ns1"`
}
func main () {
// Logging dix log
dix.Logging(true)
// Binding type values using namespace `ns1`
dix.Binding[string]("stringValue", "ns1")
dix.Binding[int](100, "ns1")
// Call DI method make a di object
x, err := dix.DI[X](context.Background())
if err != nil {
// ...
}
// Print x
fmt.Println(x.Field1, x.Field2)
}
// StringerImpl implement fmt.Stringer
type StringerImpl struct{
Field string `dix:"from:?"` // Will be automatic di
}
func (StringerImpl) String() string {
return "string_impl"
}
// X is a target di struct
type X struct {
Field string `dix:"from:?"`
FieldS fmt.Stringer `dix:"from:?"` // Will be use `StringerImpl{}` and automatic di StringerImpl's fields
FieldY Y `dix:"from:?"` // This field Y struct will be automatic di
}
// Y is a target di struct
type Y struct {
Field string `dix:"from:?"` // This field will be automatic injection values
}
func main () {
// Logging dix log
dix.Logging(true)
// Binding type values using namespace `def`
dix.Binding[string]("stringValue")
dix.Binding[fmt.Stringer](StringerImpl{})
// Call DI method make a di object
x, err := dix.DI[X](context.Background())
if err != nil {
// ...
}
// Print x
fmt.Println(x.Field, x.FieldS.Field, x.FieldY.Field)
}
// X is a target di struct
type X struct {
Field string `dix:"from:?"`
FieldY Y `dix:"from:?"` // This field Y struct will be automatic di
}
// Y is a target di struct
type Y struct {
Field string `dix:"from:?"` // This field will be automatic injection values
FieldX X `dix:"from:?"` // Cycled dependency field
}
func main () {
// Logging dix log
dix.Logging(true)
// Binding type values using namespace `def`
dix.Binding[string]("stringValue")
// Call DI method make a di object
x, err := dix.DI[X](context.Background())
if err != nil {
// !!! Will be get a cycled dependency di error
}
}
type XFieldProvider struct{}
func (XFieldProvider) Symbol() string {
return "xfp"
}
func (XFieldProvider) Provide(ctx context.Context, tag *Tag) (any, error) {
// You can obtain custom tags in the structure when calling DI
tagValue, ok := tag.GetCustomize("kind")
if ok && tagValue == "kind01" {
return "kind01", nil
}
return "x_field", nil
}
// X is a target di struct
type X struct {
Field1 string `dix:"from:xfp;kind:kind01"`
Field2 string `dix:"from:xfp;kind:kind02"`
}
func main () {
// Logging dix log
dix.Logging(true)
// Binding Provider using namespace `def`
dix.Binding[dix.Provider](XFieldProvider{})
// Call DI method make a di object
x, err := dix.DI[X](context.Background())
if err != nil {
// ...
}
// Print x
fmt.Println(x.Field1, x.Field2)
}
// X is a target di struct
type X struct {
Field0 string `dix:"from:?"` // Zero value ''
Field1 int `dix:"from:?"` // Zero value 0
Field2 []string `dix:"from:?;slice_len:3;slice_cap=3"` // Use slice_len and slice_cap like `make([]string, 3, 3)`
Field3 [5]string `dix:"from:?"` // Like `new([5]string)`
Field4 map[string]string `dix:"from:?;map_size:3"` // Use map_size like `make(map[string]string, 3)`
Field5 chan string `dix:"from:?;chan_buf:3"` // Use chan_buf like `make(chan string, 3)`
}
func main () {
// Logging dix log
dix.Logging(true)
// Call DI method make a di object
x, err := dix.DI[X](context.Background())
if err != nil {
// ...
}
}
FAQs
Unknown package
Did you know?
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.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.