Strict Runtime Dependency Injection for Golang

wire

GoDoc Build Status Go Report Card Maintainability Test Coverage FOSSA Status

Wire is runtime depedency injection/wiring for golang. It's designed to be strict to avoid your go application running without proper dependency injected.

Features:

  • Strictly validates dependency and prevents missing or ambiguous dependency.
  • Check againts possible forgotten wire tag.
  • Easily connect and resolve object anywhere.
  • Annotates ambiguous interface type using connection name or implementation name.

Install

go get github.com/Fs02/wire

Example

package wire_test

import (
	"fmt"

	"github.com/Fs02/wire"
)

type Listener struct{}

func (listener Listener) Next() string {
	return "system"
}

type Printer interface {
	Exec(string) error
}

type SystemPrint struct {
	App string `wire:""`
}

func (systemPrint SystemPrint) Exec(msg string) error {
	fmt.Println("[" + systemPrint.App + "] System: " + msg)
	return nil
}

type UserPrint struct {
	App    string `wire:""`
	Target string
}

func (userPrint UserPrint) Exec(msg string) error {
	fmt.Println("[" + userPrint.App + "]" + userPrint.Target + ": " + msg)
	return nil
}

type Service struct {
	// Each of `wire`` tag below indicate fields to be wired with apporpriate component.
	// value inside `wire` tag indicate the name of the component and optionally it's type.
	// `wire` with empty value will be wired with default value (named using empty string).
	// Ambiguous field can be resolved by adding it's type, name or both (separated using comma) to the `wire` tag.
	// Don't worry if you forgot to add wire tag to an interface or a pointer, wire will warn you if any nil field are found.
	// To ignore wiring on specific field, you can use add `wire:"-"`.
	Listener     Listener `wire:""`
	SystemPrint  Printer  `wire:",SystemPrint"`
	FooUserPrint Printer  `wire:"foo"`
	BooUserPrint Printer  `wire:"boo,UserPrint"`
}

func (service Service) Update() error {
	switch service.Listener.Next() {
	case "system":
		return service.SystemPrint.Exec("hello from system")
	case "user-foo":
		return service.FooUserPrint.Exec("hello from foo")
	case "user-boo":
		return service.BooUserPrint.Exec("hello from boo")
	default:
		return nil
	}
}

func init() {
	// add components to be wired by the library.
	// wire all components only once and as early as possible.
	wire.Connect("CoolApp")
	wire.Connect(Listener{})                       // we don't need to pass by reference here, since it doesn't require any wiring.
	wire.Connect(&SystemPrint{})                   // we need to pass by reference it to allow wiring, wire will panic if we pass by value.
	wire.Connect(&UserPrint{Target: "foo"}, "foo") // wire a UserPrint named by "foo".
	wire.Connect(&UserPrint{Target: "boo"}, "boo") // wire a UserPrint named by "boo", wire will panic if there's duplicate components detected.
	wire.Connect(&Service{})

	// Apply wiring
	wire.Apply()
}

func Example() {
	// Resolve a service component to be used later.
	var service Service
	wire.Resolve(&service)

	service.Update()
	// Output: [CoolApp] System: hello from system
}

License

Released under the MIT License

FOSSA Status

Owner
Muhammad Surya
Writing beautiful codes.
Muhammad Surya
Similar Resources

Compile-time Dependency Injection for Go

Wire: Automated Initialization in Go Wire is a code generation tool that automates connecting components using dependency injection. Dependencies betw

Jan 2, 2023

A dependency injection library that is focused on clean API and flexibility

Dependency injection DI is a dependency injection library that is focused on clean API and flexibility. DI has two types of top-level abstractions: Co

Oct 13, 2022

Golang PE injection on windows

GoPEInjection Golang PE injection on windows See: https://malwareunicorn.org/workshops/peinjection.html Based on Cryptowall's PE injection technique.

Jan 6, 2023

two scripts written in golang that will help you recognize dependency confusion.

two scripts written in golang that will help you recognize dependency confusion.

two scripts written in golang that will help you recognize dependency confusion.

Mar 3, 2022

golang auto wire code generator

Go-AutoWire helps you to generate wire files with easy annotate 中文文档 this project is base on wire but it did simplify the wire usage and make wire muc

Dec 2, 2022

golang-runtime-di is a framework for runtime dependency injection in go

golang-runtime-di description golang-runtime-di is a framework for runtime dependency injection in go. usage quickstart add it to your go.mod: go get

Aug 1, 2022

Golang JSON decoder supporting case-sensitive, number-preserving, and strict decoding use cases

Golang JSON decoder supporting case-sensitive, number-preserving, and strict decoding use cases

Dec 9, 2022

How we can run unit tests in parallel mode with failpoint injection taking effect and without injection race

This is a simple demo to show how we can run unit tests in parallel mode with failpoint injection taking effect and without injection race. The basic

Oct 31, 2021

An additive dependency injection container for Golang.

Alice Alice is an additive dependency injection container for Golang. Philosophy Design philosophy behind Alice: The application components should not

Oct 16, 2022

Minimalistic, pluggable Golang evloop/timer handler with dependency-injection

Anagent Minimalistic, pluggable Golang evloop/timer handler with dependency-injection - based on codegangsta/inject - go-macaron/inject and chuckpresl

Sep 27, 2022

Generated dependency injection containers in go (golang)

Generated dependency injection containers in go (golang)

Generation of dependency injection containers for go programs (golang). Dingo is a code generator. It generates dependency injection containers based

Dec 22, 2022

hiboot is a high performance web and cli application framework with dependency injection support

Hiboot - web/cli application framework About Hiboot is a cloud native web and cli application framework written in Go. Hiboot is not trying to reinven

Nov 20, 2022

🛠 A full-featured dependency injection container for go programming language.

DI Dependency injection for Go programming language. Tutorial | Examples | Advanced features Dependency injection is one form of the broader technique

Dec 31, 2022

A reflection based dependency injection toolkit for Go.

⚒️ dig A reflection based dependency injection toolkit for Go. Good for: Powering an application framework, e.g. Fx. Resolving the object graph during

Jan 1, 2023

Go Dependency Injection Framework

Dingo Dependency injection for go Hello Dingo Dingo works very very similiar to Guice Basically one binds implementations/factories to interfaces, whi

Dec 25, 2022

A dependency injection based application framework for Go.

🦄 Fx An application framework for Go that: Makes dependency injection easy. Eliminates the need for global state and func init(). Installation We rec

Jan 3, 2023

Simple Dependency Injection Container

Simple Dependency Injection Container

🪣 gocontainer gocontainer - Dependency Injection Container 📖 ABOUT Contributors: Rafał Lorenz Want to contribute ? Feel free to send pull requests!

Sep 27, 2022

Simple and yet powerful Dependency Injection for Go

Simple and yet powerful Dependency Injection for Go

goioc/di: Dependency Injection Why DI in Go? Why IoC at all? I've been using Dependency Injection in Java for nearly 10 years via Spring Framework. I'

Dec 28, 2022

Dependency Injection and Inversion of Control package

Linker Linker is Dependency Injection and Inversion of Control package. It supports the following features: Components registry Automatic dependency i

Sep 27, 2022
Comments
  • Add license scan report and status

    Add license scan report and status

    Your FOSSA integration was successful! Attached in this PR is a badge and license report to track scan status in your README.

    Below are docs for integrating FOSSA license checks into your CI:

An additive dependency injection container for Golang.

Alice Alice is an additive dependency injection container for Golang. Philosophy Design philosophy behind Alice: The application components should not

Oct 16, 2022
Generated dependency injection containers in go (golang)
Generated dependency injection containers in go (golang)

Generation of dependency injection containers for go programs (golang). Dingo is a code generator. It generates dependency injection containers based

Dec 22, 2022
🛠 A full-featured dependency injection container for go programming language.

DI Dependency injection for Go programming language. Tutorial | Examples | Advanced features Dependency injection is one form of the broader technique

Dec 31, 2022
A reflection based dependency injection toolkit for Go.

⚒️ dig A reflection based dependency injection toolkit for Go. Good for: Powering an application framework, e.g. Fx. Resolving the object graph during

Jan 1, 2023
Go Dependency Injection Framework

Dingo Dependency injection for go Hello Dingo Dingo works very very similiar to Guice Basically one binds implementations/factories to interfaces, whi

Dec 25, 2022
A dependency injection based application framework for Go.

?? Fx An application framework for Go that: Makes dependency injection easy. Eliminates the need for global state and func init(). Installation We rec

Jan 3, 2023
Simple Dependency Injection Container
Simple Dependency Injection Container

?? gocontainer gocontainer - Dependency Injection Container ?? ABOUT Contributors: Rafał Lorenz Want to contribute ? Feel free to send pull requests!

Sep 27, 2022
Simple and yet powerful Dependency Injection for Go
Simple and yet powerful Dependency Injection for Go

goioc/di: Dependency Injection Why DI in Go? Why IoC at all? I've been using Dependency Injection in Java for nearly 10 years via Spring Framework. I'

Dec 28, 2022
Dependency Injection and Inversion of Control package

Linker Linker is Dependency Injection and Inversion of Control package. It supports the following features: Components registry Automatic dependency i

Sep 27, 2022
Compile-time dependency injection for Go

Dihedral Dihedral is a compile-time injection framework for Go. Getting started > go get -u github.com/dimes/dihedral Create a type you want injected

Jun 1, 2022