digo tool address:/werbenhu/digo
characteristic
- Use comments
- Automatic code generation
- Automatic detection of cyclic dependencies
- Compilation-era dependency injection
- Automatic initialization
- Supports instance group management
Start quickly
For more examples, please refer to:examples
Writing code and annotations
package main import ( "log" "/werbenhu/digo" ) // @provider({"id":""}) func NewDbUrl() string { return "localhost:3306" } type Db struct { url string } // @provider({"id":""}) // @inject({"param":"url", "id":""}) func NewDb(url string) *Db { return &Db{ url: url, } } type App struct { Db *Db } // @provider({"id":""}) // @inject({"param":"db", "id":""}) func NewApp(db *Db) *App { return &App{ Db: db, } } func (a *App) Start() { ("app strat, db:%s\n", ) } func main() { app, err := ("") if err == nil { app.(*App).Start() } }
Install the digogen tool
go install /werbenhu/digo/[email protected]
Generate dependency injection code
Open the command line and execute the following command,digogen
It will be automatically generated based on the annotationSource code file.
digogen
Run the code
go run .\ .\
Note details
@provider
@provider annotation means that it is an instance provider, which is a singleton
Example
// @provider({"id":""})
Supported parameters:
parameter | type | Necessary | illustrate |
---|---|---|---|
id | string | yes | The id of the instance |
If you get the instance,(providerId)
You can get an instance of a provider
app, err := ("") if err == nil { app.(*App).Start() }
@inject
The @inject annotation means injecting an instance into a certain parameter. The @inject annotation must exist at the same time as one of @provider or @group.
Example
// @inject({"param":"db", "id":""})
Supported parameters:
parameter | type | Necessary | illustrate |
---|---|---|---|
param | string | yes | Indicate which parameter needs to be injected into an instance |
id | string | yes | Indicate the instance id that needs to be injected |
pkg | string | no | This parameter requires the introduction of specific packages |
When should pkg be used, for example, we need to introduce a package/xxx/tool/v1
, This is how we use * when using package names, instead of *, so we need to display the instructions that we need to import./xxx/tool/v1
Bag
// @inject({"param":"tool", "id":"", "pkg":"/xxx/tool/v1"})
@group
@group annotation means registering an instance to a group
Example
// @group({"id":""})
Supported parameters:
parameter | type | Necessary | illustrate |
---|---|---|---|
id | string | yes | Group id |
If you get all instances of the group,(groupId)
All instances of the group can be obtained
ctrls, err := ("") if err == nil { for _, controller := range ctrls { // TODO: } }
This is the end of this article about the detailed explanation of the use of Golang dependency injection tool digo. For more related Golang dependency injection content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!