Strict Runtime Dependency Injection for Golang

Overview

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

You might also like...
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

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

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.

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.

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

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

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

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

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

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

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

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

🛠 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

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

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

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

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!

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'

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

Comments
Releases(v1.0.2)
Owner
Muhammad Surya
Writing beautiful codes.
Muhammad Surya
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

Minjie Zha 51 Oct 16, 2022
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

null 87 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

Goava 169 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

Uber Go 2.9k 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

Flamingo 153 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

Uber Go 3.4k Jan 3, 2023
Simple Dependency Injection Container

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

Rafał Lorenz 18 Sep 27, 2022
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'

Go IoC 231 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

Logrange 36 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

null 77 Jun 1, 2022