This library generates constructor functions with dependancy code for struct and interfaces.
The library is inspired by google wire
Download and install using following commands
go get github.com/siddhesh-tamhanekar/di@v0.0.3
go install github.com/siddhesh-tamhanekar/di/cmd/di
Execute following command for generating dependancies in your project.
<goroot>/bin/di
go run .
- Create file called di.go (this file can be declared in every package where as per your need)
go:build exclude
to ignore di.go while compiling project.- This file will contain the code which tells library about how the dependancies should be resolved.
- we can use the library methods (mentioned below) to declare the ependancies.
- Once di.go is ready we can run
<goroot>/bin/di.go
to generate dependancies. - Refer example directory for more details.
Function | Usage |
---|---|
Share | di.Share(yourStruct{},db) the second parameter is the package level variable which need to use while resolving dependancy it will act like a signleton |
Build | di.Build(yourStruct{}) build method will create constructor function for given struct |
Bind | di.Bind(yourInterface, targetStruct{} this will bind Interface to struct |
BindEnv | di.BindEnv(yourInterface, targetStruct{},env) this will bind Interface to struct when ENV environment variable is set to env string |
All the functions generated with Build
method call will start with New
keyword followed by struct name for e.g. generated function for UserRepository
struct will be NewUserRepository() UserRepository
All the functions genertated with Bind
method call will start with New
keyword followed by interface name for e.g. generated function for UserCreator
interface will be New UserCreator() UserCreator
This library is still in beta and needs to handle the edge case scenerios. pls feel free to open issues and pull request to enrich the library.